这道题的基本思路比较简单,就是遇到重元素以后进行删除。另外,就是指针操作比较多,需要注意。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
class Solution { public: ListNode* deleteDuplication(ListNode* pHead) { ListNode* startNode=new ListNode(-1); startNode->next=pHead; ListNode* pSlow=startNode; ListNode* pFast=pHead; while(pSlow!=NULL && pFast!=NULL && pFast->next!=NULL){ if(pFast->val!=pFast->next->val){ pSlow=pSlow->next; pFast=pSlow->next; }else{ while(pFast->next!=NULL && pFast->val==pFast->next->val){ ListNode* tp=pFast->next; pFast->next=tp->next; delete tp; } pSlow->next=pFast->next; delete pFast; pFast=pSlow->next; } } return startNode->next; } };
|