题解 | #删除链表中重复的结点#
删除链表中重复的结点
http://www.nowcoder.com/practice/fc533c45b73a41b0b44ccba763f866ef
新增一个头节点的思想值得学习 vHead
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
*/
class Solution {
public:
ListNode* deleteDuplication(ListNode* pHead) {
if(!pHead){
return NULL;
}
ListNode *vHead=new ListNode(-1),*pre=vHead,*cur=pHead;
vHead->next=pHead;
while(cur){
if(cur->next&&cur->val==cur->next->val){
cur=cur->next;
while(cur->next&&cur->val==cur->next->val){
cur=cur->next;
}
cur=cur->next;
pre->next=cur;
}
else{
pre=cur;
cur=cur->next;
}
}
return vHead->next;
}
};