码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode OJ:Remove Duplicates from Sorted List (排好序的链表去重)

时间:2015-10-14 21:15:55      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:

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.

 

简单的链表去重而已啊,遍历一边就实现了:

 1 class Solution {
 2 public:
 3     ListNode* deleteDuplicates(ListNode* head) {
 4         ListNode * p = head;
 5         if(p == NULL || p->next == NULL)
 6             return p;
 7         ListNode * prev = p;
 8         p = p->next;
 9         while(p!=NULL){
10             if(p->val == prev->val){
11                 prev->next = p->next;
12             }else{
13                 prev = p;
14             }
15             p = p->next;
16         }
17         return head;
18     }
19 };

 

LeetCode OJ:Remove Duplicates from Sorted List (排好序的链表去重)

标签:

原文地址:http://www.cnblogs.com/-wang-cheng/p/4878798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!