• + 0 comments

    Since we know from contrains:

    Constraints: The data elements of the linked list argument will always be in non-decreasing order.

    The same number should be adjacent, therefore we just need to remove (that is, skip) the adjacent duplicate numbers.

    def removeDuplicates(self,head):
        #Write your code here
        if head != None and head.next != None:
            cur = head
            new_node = cur
            new_data = cur.data
            cur = cur.next
            while cur != None:
                # duplicate, skip
                if cur.data != new_data:
                    # new value, linked nodes and update
                    new_node.next = cur
                    new_node = cur
                    new_data = cur.data
                cur = cur.next 
            new_node.next = None
        return head