标签:list
Given a sorted linked list, delete all duplicates such that each element appear only once.
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
http://www.lintcode.com/en/problem/remove-duplicates-from-sorted-list/
ListNode *deleteDuplicates(ListNode *head) {
// write your code here
if (head == NULL) {
return head;
}
ListNode *dummyNode = head;
// dummyNode = head;
while (dummyNode != NULL) {
if (dummyNode->next != NULL && dummyNode->val == dummyNode->next->val) {
ListNode *temp = dummyNode->next;
dummyNode->next = dummyNode ->next->next;
delete temp;
} else {
dummyNode = dummyNode->next;
}
}
return head;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
Remove Duplicates from Sorted List
标签:list
原文地址:http://blog.csdn.net/richard_rufeng/article/details/46797321