Python List

Author Avatar
Cesare (MINGJU LI) Dec 09, 2017

This journal aims to describe a problem of PYTHON LIST.

The decription of the problem could be found here from LEETCODE.

Description

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.

CODE

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def deleteDuplicates(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        dummyHead=ListNode(0)
        dummyHead.next=head
        curNode=dummyHead

        while curNode!=None:
            nextNode=curNode.next
            if nextNode!=None and nextNode.next!=None and nextNode.val==nextNode.next.val:
                while nextNode!=None and nextNode.next!=None and nextNode.val==nextNode.next.val:
                    nextNode=nextNode.next
                # lastNode.next=nextNode.next
                curNode.next=nextNode.next;
            else:
                curNode=curNode.next

        return dummyHead.next