删除链表中重复元素,略简单。
/**
* 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 || head->next==NULL)
return head;
ListNode *pre = head;
ListNode *cur = head->next;
while(cur){
if(pre->val == cur->val){
ListNode *post = cur->next;
pre->next = post;
delete cur;
cur = post;
}
else{
pre = cur;
cur = cur->next;
}
}
return head;
}
};