-
- Title Description
- Link Address
- Solution
Title Description
Given a sorted linked list, delete all nodes that has duplicate numbers, leaving only distinct numbers from the original List.
Example
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
Link Address
http://www.lintcode.com/en/problem/remove-duplicates-from-sorted-list-ii/
Solution
ListNode*Deleteduplicates (ListNode*Head) {//Write your code here if(Head== NULL) {returnHead } ListNode*Dummynode= NewListNode (); Dummynode -Next=Head ListNode*Or:=Dummynode; ListNode*Cur=Or: -Next while(cur!= NULL &&Cur -Next!= NULL) {if(cur -Val==Cur -Next -Val) {ListNode*Temp=Cur while(Temp!= NULL &&Temp -Next!= NULL &&Temp -Val==Temp -Next -Val) {ListNode*Del=Temp -Next Temp -Next=Del -Next Delete del; } Pre -Next=Temp -Next Cur=Pre -Next Delete temp; }Else{Pre=Cur Cur=Cur -Next } }returnDummynode -Next }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Remove duplicates from Sorted List II