给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
示例 1:
输入: 1->2->3->3->4->4->5 输出: 1->2->5
示例 2:
输入: 1->1->1->2->3 输出: 2->3
新设置一个头节点
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head)
{
if(head == NULL)
return head;
ListNode* newHead = new ListNode(0);
newHead ->next = head;
ListNode* last = newHead;
ListNode* current = head;
ListNode* next = head ->next;
while(next)
{
if(next ->val == current ->val)
{
while(next && next ->val == current ->val)
{
next = next ->next;
}
if(next)
{
current = next;
next = next ->next;
last ->next = current;
}
else
last ->next = next;
}
else
{
last ->next = current;
last = last ->next;
current = next;
next = next ->next;
}
}
return newHead ->next;
}
};