Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
Example 1:
Input: 1->2->3->3->4->4->5
Output: 1->2->5
Example 2:
Input: 1->1->1->2->3
Output: 2->3
C++
ListNode* deleteDuplicates(ListNode* head) {
ListNode* cur = head;
ListNode* pre = new ListNode(-1);
ListNode* preHead = pre;
pre->next = cur;
while(pre->next&&pre->next->next)
{
if(pre->next->val == pre->next->next->val)
{
ListNode* pcheck = pre->next;
int k = pcheck->val;
while(pcheck&&pcheck->val == k)
{
pcheck = pcheck->next;
delete pre->next;
pre->next = pcheck;
}
}
else
pre = pre->next;
}
return preHead->next;
}