2046. Sort Linked List Already Sorted Using Absolute Values - Detailed Explanation
Problem Statement
You’re given the head of a singly‑linked list that is sorted in non‑decreasing order by the absolute values of its nodes. Your task is to reorder the list so that it is sorted in non‑decreasing order by the actual values of its nodes, then return the new head.
Example 1
Input: head = [0,2,-5,5,10,-10]
Output: [-10,-5,0,2,5,10]
Explanation:
Absolute‑sorted: [0,2,-5,5,10,-10]
Value‑sorted: [-10,-5,0,2,5,10]
Example 2
Input: head = [0,1,2]
Output: [0,1,2]
Explanation: list is already sorted by value.
Example 3
Input: head = [1]
Output: [1]
Explanation: single node.
Constraints
- The number of nodes is in the range [1, 10⁵].
- –5000 ≤ Node.val ≤ 5000
- Input list is sorted by non‑decreasing absolute values.
Hints
- Negative numbers appear in reverse order among themselves (because their absolutes were sorted).
- You can “pluck out” any negative node and insert it at the head in O(1).
Brute‑Force Approaches
- Insertion Sort on Linked List (O(n²))
- Iterate through the list, for each node remove it and insert into the proper spot in a new sorted list.
- Merge Sort on Linked List (O(n log n))
- Standard divide‑and‑conquer list sort.
Optimal O(n) Head‑Insertion Approach
Because all negative values are in reverse order at the front portion of the list, you can scan once and move each negative node to the head as you see it:
- Initialize two pointers:
prev = headcurr = head.next
- While
curris not null:- If
curr.val < 0:- Splice
currout:prev.next = curr.next - Insert
currat front:curr.next = head head = curr - Advance
curr = prev.next
- Splice
- Else (non‑negative):
prev = currcurr = curr.next
- If
- Return
head.
Step‑by‑Step Walkthrough
Take [0, 2, –5, 5, 10, –10]:
- Start
head=0→2→…,prev=0,curr=2→ 2≥0 so move both. prev=2,curr=–5→ negative: splice out and insert at front → head becomes –5→0→2→…; nowcurr=prev.nextpoints to 5.- 5≥0 → advance → now
prev=5,curr=10. - 10≥0 → advance → now
prev=10,curr=–10. - –10<0 → splice & insert → head=–10→–5→0→2→5→10;
curr = prev.next = null. - Done.
Complexity Analysis
- Time: O(n) — single pass.
- Space: O(1) — in‑place.
Python Code
Python3
Python3
. . . .
Java Code
Java
Java
. . . .
Common Mistakes
- Forgetting to update
currafter a head‑insertion (it must beprev.next). - Moving
prevwhen you shouldn’t (only move it when no insertion happens).
Edge Cases
- Single‑node list → return as is.
- All non‑negative or all non‑negative except one negative at end.
- Mixed zeroes and negatives.
Related Problems
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-
GET YOUR FREE
Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
297. Serialize and Deserialize Binary Tree - Detailed Explanation
Learn to solve Leetcode 297. Serialize and Deserialize Binary Tree with multiple approaches.
273. Integer to English Words - Detailed Explanation
Learn to solve Leetcode 273. Integer to English Words with multiple approaches.
133. Clone Graph - Detailed Explanation
Learn to solve Leetcode 133. Clone Graph with multiple approaches.
2735. Collecting Chocolates - Detailed Explanation
Learn to solve Leetcode 2735. Collecting Chocolates with multiple approaches.
503. Next Greater Element II- Detailed Explanation
Learn to solve Leetcode 503. Next Greater Element II with multiple approaches.
75. Sort Colors - Detailed Explanation
Learn to solve Leetcode 75. Sort Colors with multiple approaches.
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
4.6
(69,299 learners)
$197
New

Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
(1,107 learners)
$78
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
(26,683 learners)
$78
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.