Remove Duplicates from Sorted List II
Desicription
Given a sorted linked list, delete all nodes that have 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
.
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 class Solution { public: trueListNode* deleteDuplicates(ListNode* head) { ListNode* fake_head = new ListNode(0); fake_head>next = head; ListNode* pre = fake_head; ListNode* now = head; while(now){ while(now>next && now>val == now>next>val) now = now>next; if(pre>next == now) pre = pre>next; else pre>next = now>next; now = now>next; } return fake_head>next; } };
