LeetCode 83. 删除排序链表中的重复元素(尾插法)
原题链接
简单
作者:
Value
,
2020-09-01 10:38:14
,
所有人可见
,
阅读 632
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
ListNode* dummy = new ListNode(-1);
dummy->next = head;
if(!head) return head;
ListNode* pre = head, *r = head, *p = head->next;
while(p){
if(pre->val == p->val) p = p->next;
else{
r->next = p;
pre = p;
r = p;
p = p->next;
}
}
r->next = NULL;
return dummy->next;
}
};