Sort an Absolute-Value-Sorted Linked List
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) → ListNodeExamples
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^5nodes. - Absolute values are nondecreasing along the input list.