- 删除排序链表中的重复元素 II
给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
示例 1:
输入: 1->2->3->3->4->4->5
输出: 1->2->5
示例 2:
输入: 1->1->1->2->3
输出: 2->3
直接一点了,也没有注释,有点抱歉;
结构体
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
Solution类
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* p, * tail;
ListNode* newList_dummy_head = new ListNode(0);
int count(0);
if (!head || !head->next) return head;
p = head;
tail = newList_dummy_head;
while (p->next)
{
if (!count && p->val != p->next->val)
{
tail->next = new ListNode(p->val);
tail = tail->next;
}
else if (count && p->val != p->next->val)
{
count = 0;
}
else
{
count++;
}
p = p->next;
}
if (!count)
{
tail->next = new ListNode(p->val);
}
return newList_dummy_head->next;
}
};