My simple python solution


  • 0
    D
    # 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
            """
            dummy = ListNode(None)
            dummy.next = head
            
            tail = dummy
            current = head
            repeat = False
     
    
            
            while current and current.next:
                if current.val == current.next.val:
                    repeat = True
                    current = current.next
                else:
                    if repeat:
                        tail.next = current.next
                        current = current.next
                        repeat = False
                    else:
                        tail = tail.next
                        current = current.next
            # current may be at the end of the list and repeat exist
            if repeat:
                tail.next = None
                        
    
                    
            return dummy.next
    

Log in to reply
 

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