典型的链表删除操作。
/**
* 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) {
// Note: The Solution object is instantiated only once and is reused by each test case.
for(ListNode *cur = head, *prev = NULL; cur != NULL;){
ListNode *next = cur->next;
if(cur != NULL && prev != NULL && cur->val == prev->val){
prev->next = cur->next;
delete cur;
}
else prev = cur;
cur = next;
}
return head;
}
};