https://oj.leetcode.com/problems/remove-duplicates-from-sorted-list-ii/
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:
This problem is similar to the previous remove duplicates from Sorted list, but uses different ideas. The Remove duplicates from Sorted List is the current node compared to the previous value, and if it is larger than the previous one, join. The main point is that if the node is larger than the current node and the current node is smaller than the previous one, join. The code is as follows.
/*** Definition for singly-linked list. * public class ListNode {* int val; * ListNode Next; * ListNode (int X) {* val = x; * next = NULL; *} *}*/ Public classSolution { PublicListNode deleteduplicates (ListNode head) {if(Head = =NULL){ return NULL; } ListNode Dummynode=NewListNode (head.val-1); Dummynode.next=Head; ListNode Prenode=Dummynode; ListNode CurrentNode=Head; ListNode NextNode=Head.next; ListNode Keepnode=Prenode; while(NextNode! =NULL){ if(Prenode.val < Currentnode.val && Currentnode.val <nextnode.val) {Keepnode.next=CurrentNode; Keepnode=Keepnode.next; } Prenode=Prenode.next; CurrentNode=Currentnode.next; NextNode=Nextnode.next; } //The loop ends when the Prenode is the penultimate node, currentnode the last node if(Prenode.val <currentnode.val) {Keepnode.next=CurrentNode; Keepnode=Keepnode.next; } Keepnode.next=NULL; returnDummynode.next; }}
The key to this problem is to keep three pointers at all times, only the median value is greater than the previous one and less than the back, to join the middle node. Also pay attention to the last round, Prenode is the penultimate node, currentnode as the last node, the time to add a second judgment. Finally, it is important to note that Keepnode.next is set to NULL, otherwise it encounters 1-1 error.
The following code is another implementation with a slightly different idea, but the code is much more complex. The current node is added to the current node if it is larger than the current node, compared to the latter node. If equal, the current value as a repeating value (Invalidvalue), so in the judgement 1-3-3-4-4 such a situation of the second 3 o'clock, although the following 4 is greater than the current 3, but 3==invalidvalue, so 3 can not join.
/*** Definition for singly-linked list. * public class ListNode {* int val; * ListNode Next; * ListNode (int X) {* val = x; * next = NULL; *} *}*/ Public classSolution { PublicListNode deleteduplicates (ListNode head) {if(Head = =NULL){ return NULL; } ListNode Dummynode=NewListNode (head.val-1); Dummynode.next=Head; ListNode Keepnode=Dummynode; ListNode Traversenode=Dummynode.next; intInvalidval =Dummynode.val; while(Traversenode! =NULL){ //if the latter node of the Traversenode is empty if(Traversenode.next = =NULL){ //See if Traversenode itself is not a duplicate node, not just join if(Traversenode.val! =invalidval) {Keepnode.next=Traversenode; Keepnode=Keepnode.next; Traversenode=Traversenode.next; }Else{Traversenode=Traversenode.next; } }Else{ //Traversenode The latter node is greater than it, add it, and note 1-3-3-4-4,traversenode in the second 3 case//so to add a invalidval record the current duplicate value, cannot join if(TraverseNode.next.val > Traversenode.val && traversenode.val! =invalidval) {Keepnode.next=Traversenode; Keepnode=Keepnode.next; Traversenode=Traversenode.next; }Else{//if the latter node of Traversenode equals itInvalidval =Traversenode.val; Traversenode=Traversenode.next; } }} keepnode.next=NULL; returnDummynode.next; }}
Remove duplicates from Sorted List II