The question is to remove duplicates from sorted list and the code is :
/**
* 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 NULL;
for (ListNode* prev = head, *cur = head->next; cur; cur = cur->next)
{
if (prev->val == cur->val)
{
prev->next = cur ->next;
delete cur;
}
else
{
prev = cur;
}
}
return head;
}
};
The code is right ,My puzzle is :
the pointer "cur" , after "delete" , use" cur" and" cur = cur->next" in "for". Why is it OK?
Sorry to trouble, The leetcode give AC to the code , so I doubt myself, Thanks to the answers.