首页 > 其他 > 详细

[leetcode] 83. Remove Duplicates from Sorted List 解题报告

时间:2017-09-27 09:18:41      阅读:189      评论: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.

备份头指针,分3种情况判断即可

public ListNode deleteDuplicates(ListNode head) {
        if (head==null || head.next==null) return head;
        ListNode index =head;
        while (index.next!=null){
            if (index.next.val==index.val && index.next.next!=null){
                index.next=index.next.next;
            }else if (index.next.val==index.val && index.next.next==null){
                index.next=null;
            }else {
                index=index.next;
            }
        }
        return head;
    }

 

[leetcode] 83. Remove Duplicates from Sorted List 解题报告

原文:http://www.cnblogs.com/pulusite/p/7599853.html

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