问题:
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
没什么可说的。
代码:
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if (!head) return head;
ListNode *current = head;
ListNode *next = head->next;
while (next) {
while (next && next->val == current->val)
next = next->next;
current->next = next;
current = next;
next = next ? next->next : NULL;
}
return head;
}
};