代码改变世界

leetcode - Remove Duplicates from Sorted List

2013-10-19 21:57  张汉生  阅读(112)  评论(0编辑  收藏  举报

 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         // Note: The Solution object is instantiated only once and is reused by each test case.
13         ListNode * tmp = head;
14         while (tmp!=NULL && tmp->next !=NULL){
15             if (tmp->val == (tmp->next)->val)
16                 tmp->next = (tmp->next)->next;
17             else tmp = tmp->next;
18         }
19         return head;
20     }
21 };