首页 > 其他 > 详细

Remove Duplicates from Sorted List

时间:2014-02-19 15:14:23      阅读:269      评论:0      收藏:0      [点我收藏+]

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:

bubuko.com,布布扣
 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 };
bubuko.com,布布扣

Remove Duplicates from Sorted List

原文:http://www.cnblogs.com/fengyunyu/p/3554776.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!