题解 | #删除链表中重复的结点#
删除链表中重复的结点
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* head=new ListNode(-1);
head->next=pHead;
ListNode* pre=head;
while(pre)
{
ListNode* t=pre->next;
if(!t)
{
break;
}
int val=t->val;
ListNode* tmp=t->next;
if(!tmp)
{
break;
}
while(tmp)
{
if(tmp->val!=val)
{
break;
}
tmp=tmp->next;
}
if(t->next!=tmp)
{
pre->next=tmp;
}
else
{
pre=pre->next;
}
}
return head->next;
}
};