AcWing 29. 删除链表中重复的节点 (c语言)
原题链接
中等
作者:
Sky_
,
2020-09-24 16:11:49
,
所有人可见
,
阅读 1010
struct ListNode* deleteDuplication(struct ListNode* head) {
if(!head) return;
struct ListNode * dummy = (struct ListNode*) malloc(sizeof(struct ListNode));
dummy->next = head;
struct ListNode * pre = dummy;
struct ListNode * cur = head;
while(cur)
{
while(cur && pre->next->val==cur->val) cur = cur->next;
if (pre->next->next == cur) pre = pre->next; //如果下一段是连续的
else pre->next = cur; //如果下一段是之后的一段,直接接上
}
return dummy->next;
}