Remove Duplicates from Sorted List [LEETCODE]

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.

==========================================================

Nothing, patience, AC once.

 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(NULL == head) return NULL;
13         if(NULL == head->next) return head;
14         ListNode *p1 = head;
15         ListNode *p2 = head->next;
16         while(p1 && p2){
17             if(p1->val == p2->val){
18                 p1->next = p2->next;
19                 p2 = p1->next;
20             }else{
21                 p1 = p1->next;
22                 p2 = p2->next;
23             }
24             
25         }
26         return head;
27     }
28 };

 

posted @ 2013-10-14 16:37  昱铭  阅读(148)  评论(0)    收藏  举报