Accepted python code for deleting all duplicates in a linked list

  • 0

    My submission are listed bellow. Basically the code use three pointer to scan through the linked-list namely back, middle, and head. middle and head is used to determine whether duplicates exist. If so delete them. To address the case when the first several elements are duplicates. I added an element in front of the whole list. Any comments on improving it would be appreciated!

    class Solution:
        # @param head, a ListNode
        # @return a ListNode
        def deleteDuplicates(self, head):
            if (not head) or (not
                return head
                headnew= ListNode(1) # add a head node
                middle = head
                flag = 0
                while front:
                    if front.val == middle.val:
                        flag +=1
                        if flag == 0:
                            front =
                            middle = front
                            front =
                if flag>0:

Log in to reply

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