Sort Linked List Based on Actual Values
You are given a Singly Linked List of integers that is initially sorted according to the absolute values of its elements. Your task is to sort this Linked List based on the actual integer values.
The absolute value of a number x
, denoted as |x|
, is the non-negative value of x
without regard to its sign.
Input:
The first line consists of a single integer ‘T’, denoting the number of test cases. Each test case includes a single line containing the elements of the singly linked list, separated by spaces, and terminated by -1. The value -1 is not part of the list data and serves as a terminator.
Output:
For each test case, provide the sorted linked list where the elements are separated by a space, ending with -1.
Example:
Input:
1 -> -2 -> 3 -1
Output:
-2 -> 1 -> 3 -1
Constraints:
- 1 <= T <= 10
- 1 <= N <= 5 * 104
- -109 <= data <= 109 (data ≠ -1)
- The input list is guaranteed to be sorted based on absolute values.
Note:
The function should be implemented to sort the linked list. Output handling has been dealt with separately.

AnswerBot
1y
The task is to sort a singly linked list based on actual values instead of absolute values.
Traverse the linked list and store the values in an array
Sort the array using any sorting algorithm
Create a n...read more
Help your peers!
Add answer anonymously...
Josh Technology Group Front end Developer interview questions & answers
A Front end Developer was asked Q. Implement a live coding round to build a small CRUD-based application.
A Front end Developer was asked Q. Non-Decreasing Array Problem Statement Given an integer array ARR of size N, det...read more
A Front end Developer was asked Q. Diameter of a Binary Tree Problem Statement Given a binary tree, return the leng...read more
Popular interview questions of Front end Developer
A Front end Developer was asked Q1. Implement a live coding round to build a small CRUD-based application.
A Front end Developer was asked Q2. Non-Decreasing Array Problem Statement Given an integer array ARR of size N, det...read more
A Front end Developer was asked Q3. Diameter of a Binary Tree Problem Statement Given a binary tree, return the leng...read more
>
Josh Technology Group Front end Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app


Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+
Reviews
10L+
Interviews
4 Cr+
Salaries
1.5 Cr+
Users
Contribute to help millions
AmbitionBox Awards
Get AmbitionBox app

