Remove Duplicates from Sorted List II 2020-09-14 02:52

Problem Description

public static ListNode deleteDuplicates(ListNode head) {
    ListNode fake = new ListNode(0);
    fake.next = head;
    ListNode pre = fake;
    ListNode current = head;
    while (current != null && current.next != null) {
        if (current.val != current.next.val) {
            if (pre.next != current) {
                pre.next = current.next;
            } else {
                pre = pre.next;
            }
        }
        current = current.next;
    }
    if (pre.next != current) {
        pre.next = null;
    }
    return fake.next;
}
Runtime Memory
1 ms 40.9 MB

henryxi leetcode list

EOF