首页 > 代码库 > remove-duplicates-from-sorted-list

remove-duplicates-from-sorted-list

https://leetcode.com/problems/remove-duplicates-from-sorted-list/

 

    public ListNode deleteDuplicates(ListNode head) {
        int last = -1;
        ListNode store = null;
        ListNode cur = head;
        while (cur != null) {
            if (store == null) {
                store = head;
            }
            else if (cur.val != last) {
                store = store.next;
                store.val = cur.val;
            }
            last = cur.val;
            cur = cur.next;
        }
        if (store != null) {
            store.next = null;
        }
        return head;
    }

 

remove-duplicates-from-sorted-list