Thursday, June 5, 2014

LeetCode Remove Duplicates from Sorted List

public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode curr = head;
        ListNode next = head.next;
        while (next != null) {
            if (curr.val == next.val) {
                curr.next = next.next;
                next = curr.next;
            } else {
                curr = next;
                next = curr.next;
            }
        }
        return head;
    }

No comments:

Post a Comment