Given a sorted linked list, delete all nodes that has duplicate numbers, leaving only distinct numbers from the Original list.
For example,
Given 1->2->3->3->4->4->5
, return 1->2->5
.
Given 1->1->1->2->3
, return 2->3
.
Problem Solving Ideas:
In contrast to I, this problem requires that no duplicate nodes be retained. Therefore, an index record is required at the end of the node that is currently reserved, and then traversed with another node, and the duplicate values and new values are processed in the process.
The difficulty lies in the treatment of corner cases. Not long after the beginning of the brush, have not developed the habit of pre-written test, so in these special cases, spent a lot of time.
Corner Case: [1,2,2],[0,1,2,2,3,4],[1,1],[1],[1,1,2,2]
1 if(head!=NULL){2ListNode C1 =NewListNode (head.val-1);3C1.next =head;4ListNode re = c1, C2 =head;5 intCount = 0, temp =C1.next.val;6 7 while(c2!=NULL){8 if(Temp!=c2.val && count>1){9C1.next =C2; Tentemp =C2.val; OneCount = 1; A}Else if(Temp!=c2.val && Count==1){ -C1 =C1.next; -temp =C2.val; theCount = 1; -}Else -Count + +; -C2 =C2.next; + } - if(Count > 1) c1.next =NULL; + returnRe.next; A } at return NULL;
Leetcode–remove duplicates from Sorted List II (Java)