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.
难度系数:
容易
实现
ListNode *deleteDuplicates(ListNode *head) {
ListNode *next = NULL;
ListNode *curr = head;
while (curr) {
if (curr->next == NULL) {
return head;
}
next = curr->next;
if (curr->val == next->val) {
curr->next = next->next;
continue;
}
curr = curr->next;
}
return head;
}
时间: 2024-10-16 05:30:23