//给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中 没有重复出现 的数字。
//
// 示例 1:
//
// 输入: 1->2->3->3->4->4->5
//输出: 1->2->5
//
//
// 示例 2:
//
// 输入: 1->1->1->2->3
//输出: 2->3
// Related Topics 链表
// 👍 456 👎 0
/*
* 82 删除排序链表中的重复元素 II
* 2021-02-22 17:19:22
* @author oxygenbytes
*/
#include "leetcode.h"
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(!head ||!head->next) return head;
ListNode dummy(-1);
dummy.next = head;
auto ptr = &dummy;
// ptr mark end of target list
// cur mark current node
while(ptr->next){
auto cur = ptr->next;
while(cur->next && cur->next->val == cur->val){ // a-->a-->a-->b
cur = cur->next;
}
if(cur != ptr->next) ptr->next = cur->next; // a->b->c->d
else ptr = ptr->next;
}
return dummy.next;
}
};
//leetcode submit region end(Prohibit modification and deletion)