My accepted Python Solution


  • 0
    S
    # Definition for singly-linked list.
    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution:
        # @param head, a ListNode
        # @return a ListNode
        def deleteDuplicates(self, head):
            guard = ListNode(0)
            guard.next = head
            duplicated  = False
            p = head
            q = guard
            while p is not None and p.next is not None:
                while p.next is not None and p.val == p.next.val:
                    p = p.next
                    duplicated = True
                p = p.next
                if duplicated:
                    q.next = p
                else:
                    q = q.next
                duplicated = False
            return guard.next

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.