AcWing 29. 删除链表中重复的节点
原题链接
中等
作者:
Value
,
2020-09-01 19:34:12
,
所有人可见
,
阅读 349
/**
* 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) {
ListNode* dummy = new ListNode(-1);
dummy->next = head;
if(head == NULL) return head;
ListNode* p = dummy, *q = p->next;
while(q->next != NULL){
if(q->val == q->next->val) q = q->next;
else{
if(p->next == q) p = p->next, q = q->next;
else{
q = q->next;
p->next = q;
}
}
}
if(p->next != q) p->next = NULL;
return dummy->next;
}
};