/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* head) {
auto bh = new ListNode(-1);
bh->next = head;
auto from=bh;
/*
if(from->next->next=to)from=from->next;
else from->next=to;
}
*/
while (from->next) {
auto to = from->next;
while (to && from->next->val == to->val) to = to->next;
if (from->next->next == to) from = from->next;
else from->next = to;
}
return bh->next;
}
};
acwing 29. 删除链表中重复的节点
最新推荐文章于 2023-07-28 22:41:59 发布
本文介绍了一种在单链表中删除所有重复节点的方法。通过使用哑节点简化边界条件,遍历链表并检查相邻节点的值是否相同,如果发现重复则跳过这些节点直到找到唯一值的节点。这种方法保持了链表的原始顺序,同时移除了所有重复的元素。
153

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



