Skip to content

Latest commit

 

History

History
38 lines (31 loc) · 649 Bytes

Remove Duplicates from Sorted List.md

File metadata and controls

38 lines (31 loc) · 649 Bytes

Provided code

class ListNode {
    int val;
    ListNode next;
}

Solution

class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode n = head;
        while (n.next != null) {
            if (n.next.val == n.val) {
                n.next = n.next.next; // deletes ListNode
            } else {
                n = n.next;
            }
        }
        return head;
    }
}

Time/Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Links