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
.
Subscribe to see which companies asked this question
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 public: 11 ListNode* deleteDuplicates(ListNode* head) { 12 if(head==NULL) return NULL; 13 ListNode* p,*q; 14 p=head; 15 q=p->next; 16 while(q!=NULL){ 17 while(q->val==p->val) 18 { 19 if(q->next!=NULL) 20 q=q->next; 21 else { 22 p->next=NULL; 23 return head;} 24 } 25 p->next=q; 26 p=q; 27 q=p->next; 28 29 } 30 31 return head; 32 } 33 34 };
leetcode Remove Duplicates from Sorted List
原文:http://www.cnblogs.com/LUO77/p/4974377.html