800. Sort Linked List Already Sorted Using Absolute Values

Given the 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.

Now Your Turn!

Pick the correct output for the given input

What will be the correct sorted order of this Input: [0,-1,3,-4,5,-6]?

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • The number of nodes in the list is the range [1, 105].
  • -5000 <= Node.val <= 5000
  • head is sorted in non-decreasing order using the absolute value of its nodes.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* sortLinkedList(ListNode* head) {
// Your code goes here
}
};
Test Case

Input:

Head