Remove Duplicates from Sorted List
/**
* 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 || !head->next) return head;
ListNode *cp = head, *cur = head->next;
while (cur) {
if (cp->val != cur->val) {
cp->next = cur;
cp = cur;
}
cur = cur->next;
}
cp->next = NULL;
return head;
}
};