Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2,
return 1->2.
Given 1->1->2->3->3,
return 1->2->3.
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int count=0;
ListNode *p,*q;
if(!head)return NULL;
p=head;
while(p->next){
if(p->val==p->next->val){
count++;
if(count>1){
q=p->next;
p->next=p->next->next;
delete q;
}
}else{
count=0;
p=p->next;
}
}
return head;
}
};
本文介绍如何在保持链表排序的情况下,移除所有重复的节点,确保每个元素只出现一次。
733

被折叠的 条评论
为什么被折叠?



