mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-08-06 18:24:23 +08:00
758 B
758 B
题目地址
https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
思路
这道题目没有必要设置虚拟节点,因为不会删除头结点
代码
/**
* 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* p = head;
while (p != NULL && p->next!= NULL) {
if (p->val == p->next->val) {
ListNode* tmp = p->next;
p->next = p->next->next;
delete tmp;
} else {
p = p->next;
}
}
return head;
}
};