Sort a "K" Sorted Doubly Linked List
Given a doubly-linked list with N
nodes, where each node’s position deviates at most K
positions from its position in the sorted list, your task is to sort this given doubly linked list.
Example:
Let us consider K
is 3. An element at position 4 in the sorted doubly linked list can be at positions 1, 2, 3, 4, 5, 6, 7 in the given linked list because the absolute difference of all these indices with 4 is at most 3.
Input:
The input starts with an integer 'T', representing the number of test cases.
For each test case:
- A line containing integer 'K'.
- A second line with the elements of the doubly linked list separated by spaces and terminated by -1. -1 is not part of the list.
Output:
For each test case, output the sorted elements of the linked list in a single line, separated by spaces, and terminated by -1.
Constraints:
- 1 <= T <= 10
- 1 <= N <= 10000
- 1 <= K < N
Note:
All elements in the doubly linked list are distinct. A doubly linked list can be traversed in both directions, forward and backward. Implement the function as required. Printing is handled separately.
Be the first one to answer
Add answer anonymously...
Top Nagarro SDE interview questions & answers
Popular interview questions of SDE
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