题解 | #删除链表中重复的结点#
删除链表中重复的结点
http://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) {
ListNode* vhead = new ListNode(-1);
vhead->next = pHead;
ListNode* pre = vhead;
ListNode* cur = pHead;
while(cur != nullptr){
if(cur->next != nullptr && cur->val == cur->next->val){
cur = cur->next;
while(cur->next != nullptr && cur->val == cur->next->val){
cur = cur->next;
}
cur = cur->next;
pre->next = cur;
}else{
pre = cur;
cur = cur->next;
}
}
return vhead->next;
}
};
