head of a singly linked list that is sorted in non-decreasing order using the absolute values of its nodes, return the list sorted in non-decreasing order using the actual values of its nodes.
Example 1:
Input: head = [0,2,-5,5,10,-10] Output: [-10,-5,0,2,5,10] Explanation: The list sorted in non-descending order using the absolute values of the nodes is [0,2,-5,5,10,-10]. The list sorted in non-descending order using the actual values is [-10,-5,0,2,5,10].
Example 2:
Input: head = [0,1,2] Output: [0,1,2] Explanation: The linked list is already sorted in non-decreasing order.
Example 3:
Input: head = [1] Output: [1] Explanation: The linked list is already sorted in non-decreasing order.
Constraints:
[1, 105].-5000 <= Node.val <= 5000head is sorted in non-decreasing order using the absolute value of its nodes.Follow up:
O(n) time complexity?Problem Overview: The linked list is sorted by absolute values, not by the actual values. Negative numbers may appear after positives because their absolute values are smaller. Your task is to reorder the list so it becomes correctly sorted by the real values while keeping the operation efficient.
Approach 1: Convert to Array and Sort (O(n log n) time, O(n) space)
Traverse the linked list and copy all node values into an array. Sort the array using a standard sorting algorithm, then rebuild the linked list or overwrite the node values in sorted order. This approach works for any list regardless of structure and is easy to implement. However, it ignores the special property that the list is already sorted by absolute value, which means it does extra work compared to the optimal solution.
Approach 2: Head Insertion Method (O(n) time, O(1) space)
The key observation: because the list is sorted by absolute value, all negative values appear in reverse order relative to their correct sorted positions. For example, a sequence like 1 → -2 → -3 → 4 means the negatives must be moved toward the front. Iterate through the list while maintaining a pointer to the previous node. Whenever you encounter a negative node that appears after a positive segment, detach it and insert it at the head of the list. This is essentially a controlled reordering using pointer manipulation.
The process only requires pointer updates: remove the current negative node (prev.next = curr.next) and push it to the front (curr.next = head, then update head). Because each node is visited once and moved at most once, the algorithm runs in linear time. No extra memory is needed beyond a few pointers.
This technique is closely related to pointer manipulation patterns used in two pointers and in-place transformations in sorting problems on linked lists. Instead of re-sorting the entire structure, you exploit the ordering constraint already present in the input.
Recommended for interviews: The head insertion method is the expected solution. Interviewers want you to notice the special ordering by absolute value and convert it into a linear-time transformation using pointer manipulation. The array + sort approach demonstrates baseline understanding, but the O(n) in-place method shows strong linked list reasoning and algorithmic insight.
We first assume that the first node is already sorted. Starting from the second node, when we encounter a node with a negative value, we use the head insertion method. For non-negative values, we continue to traverse down.
The time complexity is O(n), where n is the length of the linked list. The space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Copy to Array and Sort | O(n log n) | O(n) | Simple implementation when constraints are small or when ignoring the absolute-value property |
| Head Insertion Method | O(n) | O(1) | Optimal solution that exploits the list being sorted by absolute values |
leetcode 2046. Sort Linked List Already Sorted Using Absolute Values - linear traversal with check • Code-Yao • 165 views views
Watch 2 more video solutions →Practice Sort Linked List Already Sorted Using Absolute Values with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor