/**
* 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 *fakehead = new ListNode(0);
if (head == NULL)
return NULL;
ListNode *pre, *cur;
fakehead->next = head;
for (pre = head, cur = head->next; cur;cur=cur->next)
{
if (pre->val == cur->val)
{
pre->next = cur->next;
ListNode *tmp = cur;
free(tmp);
}
else
{
pre = cur;
}
}
return fakehead->next;
}
};
【LeetCode】Remove Duplicates from Sorted List
最新推荐文章于 2024-01-17 14:07:10 发布