FastPrepRemove Duplicates from Sorted List

Remove Duplicates from Sorted List

Bloomberg LP logoBloomberg LP● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

Given the head of a nondecreasing singly linked list, delete repeated nodes so that each value appears exactly once. Return the head of the same list after relinking its existing nodes.

Function

deleteDuplicates(head: ListNode) → ListNode

Examples

Example 1

head = [1,1,2]return = [1,2]

The second node with value one is removed.

Example 2

head = [1,1,2,3,3]return = [1,2,3]

Each repeated run is reduced to one node.

Constraints

  • The list contains between 0 and 10^5 nodes.
  • -10^9 <= Node.val <= 10^9.
  • The list is sorted in nondecreasing order.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public ListNode deleteDuplicates(ListNode head) {
    // Write your code here.
}
head[1,1,2]
expected[1,2]
Checking account…