/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
ListNode* root = NULL;
ListNode** ppNext = &root;
while (head)
{
if (head->next == NULL || head->next->val != head->val)
{
*ppNext = head;
ppNext = &(head->next);
head = head->next;
}
else
{
int val = head->val;
do
{
head = head->next;
} while (head != NULL && head->val == val);
}
}
*ppNext = NULL;
return root;
}
};[Leetcode] Remove Duplicates from Sorted List II
最新推荐文章于 2022-04-03 22:46:44 发布
本文介绍了一种优化算法,用于处理单链表中的重复节点问题,通过逐个检查并移除重复元素,确保链表的唯一性。
711

被折叠的 条评论
为什么被折叠?



