题解 | #删除链表中重复的结点#
删除链表中重复的结点
https://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param pHead ListNode类 * @return ListNode类 */ typedef struct ListNode Node; struct ListNode* deleteDuplication(struct ListNode* pHead ) { if(pHead==NULL||pHead->next==NULL) return pHead; Node* cur=pHead; Node* pre=NULL; Node* nextnode=cur->next; while(nextnode) { if(cur->val!=nextnode->val) { pre=cur; cur=cur->next; if(nextnode) nextnode=cur->next; else nextnode=NULL; } else { while(cur->val==nextnode->val) { nextnode=nextnode->next; if(nextnode==NULL) break; } Node* tmp=NULL; if(pHead==cur) pHead=nextnode; while(cur!=nextnode) { tmp=cur->next; free(cur); cur=tmp; } if(pre) pre->next=cur; if(nextnode) nextnode=cur->next; } } return pHead; }