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...
Top Josh Technology Group Front end Developer interview questions & answers
Popular interview questions of Front end Developer
Top HR questions asked in Josh Technology Group Front end Developer
>
Josh Technology Group Front end Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app