leetcode 82. 删除排序链表中的重复元素 II
作者:互联网
/**
* 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) {
ListNode* newHead = new ListNode(0, head);
ListNode *p = newHead, *q, *t;
while(p->next) {
t = p->next;
for(q = t->next; q && q->val == t->val; q = q->next);
if(t->next == q) {
p->next = t;
p = p->next;
}
else p->next = q;
}
return newHead->next;
}
};
标签:II,ListNode,val,int,next,链表,82,newHead 来源: https://blog.csdn.net/m0_37454852/article/details/115218555