题解 | #删除链表中重复的结点#
删除链表中重复的结点
https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef
/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) { } }; */ class Solution { public: ListNode* deleteDuplication(ListNode* pHead) { if(!pHead) return nullptr; ListNode *pre = new ListNode(0); pre->next = pHead; ListNode *last = nullptr; ListNode *p = nullptr; int cur = -1; last = pre; p = pHead; cur = pHead->val; //时间复杂度O(n),空间复杂度O(1),因为是顺序的所以没必要额外记录 while(p->next) { if(p->next->val==cur) { while(p->next && p->next->val==cur) p=p->next; p=p->next; last->next=p; }else { last = p; p=p->next; } if(p) cur = p->val; else break; } return pre->next; } };
磨砂的指名者 文章被收录于专栏
怎么绘世呢?