Question: https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
Question Name: Remove Duplicates from Sorted List II
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | class Solution: # @param head, a ListNode # @return a ListNode def deleteDuplicates(self, head): # Handle special case that the list is empty if head == None: return head # Add a pseudo head node to make the process uniform tempHead = ListNode(head.val ^ 0x1) tempHead.next = head current = tempHead while current.next != None: nextVal = current.next if nextVal.next != None and nextVal.val == nextVal.next.val: # This element and its next one are the same. # They are duplicate. We are going to remove # all the nodes with this value from the list. nextVal = nextVal.next while nextVal.next != None and nextVal.val == nextVal.next.val: nextVal = nextVal.next current.next = nextVal.next else: current = current.next # Skip the pseudo head node, and return the real one. return tempHead.next |