首页 > 其他 > 详细

Remove Duplicates from Sorted List

时间:2014-02-23 07:41:39      阅读:288      评论: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.

基本思路:

这题比较简单,扫描一遍链表基本上就出来了。

public ListNode deleteDuplicates(ListNode head) {
        if(head==null)
        return null;
        ListNode pre,p;
        int temp = 0;
        int flag = 0;
        p = head;
        pre = head;
        if(p!=null)
            temp = p.val;
        p = p.next;
        while(p!=null)
        {
            if(p.val!=temp)
            {
                flag = 1;
                pre.next = p;
                pre = p;
                temp = p.val;
            }
            else
                flag = 0;
            p = p.next;
        }
        if(flag==0&&pre!=null)
            pre.next = null;
        return head;
    }


Remove Duplicates from Sorted List

原文:http://blog.csdn.net/cow__sky/article/details/19689155

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