FastPrepSort an Absolute-Value-Sorted Linked List

Sort an Absolute-Value-Sorted Linked List

Bloomberg LP logoBloomberg LP● MediumNEW GRADONSITE INTERVIEW
Learn

Problem statement

A singly linked list is sorted by nondecreasing absolute value. Rearrange its existing nodes so their integer values are in ordinary nondecreasing order and return the new head.

Function

sortAbsoluteList(head: ListNode) → ListNode

Examples

Example 1

head = [1,-2,-3,4,-5]return = [-5,-3,-2,1,4]

Negative nodes reverse magnitude order before the positive nodes.

Example 2

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

All negative nodes reverse.

Constraints

  • The list has at most 10^5 nodes.
  • Absolute values are nondecreasing along the input list.

More Bloomberg LP problems

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