题目链接:Remove Duplicates from Sorted List
题面:
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
.
struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; class Solution { public: ListNode* deleteDuplicates(ListNode* head) { if(head==NULL)return head; ListNode* newHead = head; ListNode* curr = head->next; ListNode* tmp; while( curr != NULL ) { if( curr->val != head->val ) { curr = curr->next; head = head->next; } else { tmp=curr; curr = curr->next; head->next=curr; delete tmp; } } return newHead; } };
LeetCode Remove Duplicates from Sorted List
原文地址:http://blog.csdn.net/david_jett/article/details/45938579