AcWing 29. 删除链表中重复的节点
原题链接
中等
作者:
acw_zyx
,
2021-06-14 09:58:08
,
所有人可见
,
阅读 230
/**
* 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;
ListNode* cur = dummy;
while (cur->next != NULL) {
ListNode* q = cur->next;
while (q != NULL && cur->next->val == q->val) {
q = q->next;
}
if (cur->next->next == q) {
cur = cur->next;
} else {
cur->next = q;
}
}
return dummy->next;
}
};