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
.
源代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head) {
if (head == NULL) return head;
struct ListNode* curr = head;
struct ListNode* n = head->next;
struct ListNode* tmp = NULL;
while (n != NULL) {
if (n->val == curr->val) {
curr->next = n->next;
n = n->next;
}
else {
curr = n;
n = n->next;
}
}
return head;
}