Remove Duplicates from Sorted List

看了别人讨论,双指针比较方便http://www.cnblogs.com/springfor/p/3862042.html

public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head==null|| head.next==null) return head;
        ListNode h = new ListNode(-1);
        h.next = head;
        ListNode l1 = head;
        ListNode l2 = l1.next;
        while(l2!=null){
            while(l2!=null && l1.val==l2.val){
                l2 = l2.next;
            }
            l1.next = l2;
            l1=l1.next;
        }
        return h.next;
    }
}

 

posted @ 2015-04-08 03:13  世界到处都是小星星  阅读(80)  评论(0编辑  收藏  举报