删除重复的节点。只需要遍历一遍。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
if(head==NULL) return head;
int tmp=head->val;
ListNode* p=head->next;
ListNode* pre=head;
while(p){
if(tmp==p->val){
ListNode* q=p->next;
pre->next=p->next;
p=q;
}
else{
tmp=p->val;
pre=p;
p=p->next;
}
}
return head;
}
};