Python solution


  • 0
    C
    1. Find first non-duplicate node, repoint head.
    2. Do similar thing as in Remove Duplicates from Sorted List I
    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        def newhead(self, head):
            new_head = head
            if new_head.val == new_head.next.val:
                temp = new_head.val
                while new_head and new_head.val == temp:
                    new_head = new_head.next
            return new_head
        def deleteDuplicates(self, head):
            """
            :type head: ListNode
            :rtype: ListNode
            """
            if head is None:
                return None
            pre = head
            cur = head.next
            new_head = head
            while new_head and new_head.next and new_head.val == new_head.next.val:
                head = new_head
                new_head = self.newhead(new_head)
            while cur:
                pnext = cur.next
                dup = False
                while pnext and pnext.val == cur.val:
                    pre.next = pnext.next
                    cur = pnext
                    pnext = pnext.next
                    dup = True
                if dup == True:
                    pre.next = pnext
                else:
                    pre = cur
                cur = pnext
            return new_head
                    
                    
    

Log in to reply
 

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