1. 程式人生 > >[leetcode] 83. 刪除排序鏈表中的重復元素

[leetcode] 83. 刪除排序鏈表中的重復元素

ref null let 重復元素 ret http desc pub leet

83. 刪除排序鏈表中的重復元素

鏈表操作

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) return head;
        if (head.next == null) return head;
        ListNode now = head.next;
        ListNode last = head;
        while (now != null) {
            while (now != null && now.val == last.val) {
                now = now.next;
            }
            last.next = now;
            last = now;
            if (now != null)
                now = now.next;
        }
        return head;
    }
}

[leetcode] 83. 刪除排序鏈表中的重復元素