http://oj.leetcode.com/problems/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
.
Solution:
1 //Definition for singly-linked list. 2 struct ListNode { 3 int val; 4 ListNode *next; 5 ListNode(int x) : val(x), next(NULL) {} 6 }; 7 8 class Solution { 9 public: 10 ListNode *deleteDuplicates(ListNode *head) { 11 if(head==NULL) return NULL; 12 ListNode * index =head; 13 ListNode * p=head->next; 14 while(p){ 15 if(index->val!=p->val){ 16 index=index->next; 17 index->val=p->val; 18 } 19 p=p->next; 20 } 21 ListNode * del=index->next; 22 index->next=NULL; 23 while(del){ 24 ListNode * tmp = del->next; 25 delete del; 26 del=del->next; 27 } 28 return head; 29 } 30 };
Remove Duplicates from Sorted List
原文:http://www.cnblogs.com/fengyunyu/p/3554776.html