[leetcode] 83. 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.

备份头指针,分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;
    }

 

posted @ 2017-09-27 07:11  pulusite  阅读(73)  评论(0编辑  收藏  举报