Title Description:
Given a sorted list, delete all duplicate elements, leaving only one for each element.
Have you ever encountered this problem in a real interview? Yes
Sample Example
Give 1->1->2->null
, return1->2->null
Give 1->1->2->3->3->null
, return1->2->3->null
labelLinked list
Topic Analysis:
Given a sorted list, delete all duplicate elements, leaving only one for each element.
Source:
"" "Definition of Listnodeclass ListNode (object): def __init__ (self, Val, next=none): self.val = val Self.next = Next "" "Class Solution:" " @param head:a listnode @return: A listnode" "" Def Deleteduplicates (self, head): # Write your code this if head is none: return None pre = head cur = Pre fol = Pre.next while fol.next are not None: # always find the first fol that is different from the pre value, delete all the repeating elements in the middle if pre.val = = Fol.val : fol = fol.next else: pre.next = fol pre = fol fol = pre.next # Detect if the end is equal if Pre.val = = Fol.val: pre.next = fol.next else: pre.next = fol return cur
Lintcode Python Simple class topic 112. Delete duplicate elements in a linked list