AcWing 29. 删除链表中重复的节点
原题链接
中等
作者:
NeonSean
,
2020-08-09 17:40:21
,
所有人可见
,
阅读 344
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* head) {
if (!head || !head->next) return head;
auto dump = new ListNode(-1);
dump->next = head;
ListNode* p = dump;
ListNode* q = p->next;
while(q) {
while (q->next && q->val == q->next->val) {
q = q->next;
}
if (p->next == q) {
p = p->next;
q = q->next;
}
else {
p->next = q->next;
q = p->next;
}
}
return dump->next;
}
};