/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead)
{
ListNode *vhead = new ListNode(-1);
vhead->next = pHead;
ListNode *pre = vhead, *cur = pHead;
ListNode *tmp = nullptr; //临时指针,用于释放内存
while (cur) {
if (cur->next && cur->val == cur->next->val) {
while (cur->next && cur->val == cur->next->val) {
tmp = cur;
cur = cur->next;
delete tmp;
}
tmp = cur;
cur = cur->next;
delete tmp;
pre->next = cur;
}
else {
pre = cur;
cur = cur->next;
}
}
return vhead->next;
}
};
剑指数据结构-删除链表中重复的节点
最新推荐文章于 2024-03-04 08:26:52 发布