Python Clean and Fast 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
            pre = dummy
            while head:
                dup = False
                while head.next and head.next.val == head.val:
                    dup = True
                    head = head.next
                if dup: pre.next = head.next
                else: pre = pre.next
                head = head.next
            return dummy.next

Log in to reply
 

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