题解 | #删除链表中重复的结点#
删除链表中重复的结点
http://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* remove(ListNode *p){
ListNode* t;
int val = p->val;
while(p && p->val == val){
t = p;
p = p->next;
free(t);
}
return p;
}
ListNode* deleteDuplication(ListNode* pHead) {
if(!pHead || !pHead->next) return pHead;
ListNode *cur = pHead, *next = NULL, *temp = NULL;
ListNode *res = new ListNode(0);
res->next = pHead;
cur = res;
while(cur->next){
if(cur->next && cur->next->next){
if(cur->next->val == cur->next->next->val){
temp = remove(cur->next);
cur->next = temp;
continue;
}
}
cur = cur->next;
}
return res->next;
}
};