Thursday, March 7, 2013

[LeetCode] Remove Duplicates from Sorted List

Thought:
Basically the same with from sorted array.

Code:
 public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) return head;
        ListNode current = head.next;
        ListNode index = head;
        while (current != null) {
            if (current.val == index.val) {
                index.next = current.next;
            }else {
                index = index.next;
            }
            current = current.next;
        }
        return head;
    }
}

No comments:

Post a Comment