Sort Linked List Already Sorted Using Absolute Values - Problem
You're given a singly linked list that has an interesting property: it's already sorted in non-decreasing order by the absolute values of its nodes. However, the actual values might be negative, making the list appear unsorted when considering the real values.

Your task is to sort this linked list by the actual values (not absolute values) while maintaining the non-decreasing order.

For example, if you have a list like [-5, -2, 3, 4, -6] which is sorted by absolute values [5, 2, 3, 4, 6], you need to rearrange it to [-6, -5, -2, 3, 4] sorted by actual values.

The key insight is that since the list is already sorted by absolute values, negative numbers with larger absolute values need to be moved to the front, while maintaining the relative order of positive numbers.

Input & Output

example_1.py — Basic Case
$ Input: head = [-5, -2, 3, 4, -6]
Output: [-6, -5, -2, 3, 4]
💡 Note: The original list is sorted by absolute values: |5|, |2|, |3|, |4|, |6|. After sorting by actual values, negative numbers come first in descending order of their absolute values, followed by positive numbers.
example_2.py — Only Positive Values
$ Input: head = [1, 3, 5, 7, 9]
Output: [1, 3, 5, 7, 9]
💡 Note: Since all values are positive, the list is already sorted by actual values and remains unchanged.
example_3.py — Only Negative Values
$ Input: head = [-1, -3, -5]
Output: [-5, -3, -1]
💡 Note: All values are negative, so we need to reverse the order since the original was sorted by absolute values (1 < 3 < 5), but for actual values we need -5 < -3 < -1.

Constraints

  • The number of nodes in the list is in the range [0, 104]
  • -104 ≤ Node.val ≤ 104
  • The list is sorted in non-decreasing order by absolute values
  • You must sort the list in non-decreasing order by actual values

Visualization

Tap to expand
Sort Linked List by Absolute Values INPUT Original Linked List: -5 -2 3 4 -6 Sorted by |value|: |5| |2| |3| |4| |6| Input Array: [-5, -2, 3, 4, -6] Negative Positive ALGORITHM STEPS 1 Initialize Keep head, traverse list 2 Check Each Node If negative, move to front 3 Relink Negative Nodes Detach and prepend to head 4 Continue Until End Return new head Processing Order: -5 --> -2 --> 3 --> 4 --> -6 -6 found: move to front -6 --> -5 --> -2 --> 3 --> 4 Time: O(n) | Space: O(1) FINAL RESULT Sorted Linked List: -6 -5 -2 3 4 NULL Output Array: [-6, -5, -2, 3, 4] OK - Sorted! Non-decreasing order -6 < -5 < -2 < 3 < 4 Key Insight: Since list is sorted by absolute values, negative numbers with larger |value| appear later. Moving them to front in order maintains sorted sequence. One pass, O(1) space - optimal! TutorialsPoint - Sort Linked List Already Sorted Using Absolute Values | Optimal Solution
Asked in
Google 28 Amazon 22 Microsoft 18 Meta 15
42.3K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen