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.
Sort a doubly linked list where each node's position deviates at most K positions from its position in the sorted list.
Iterate through the doubly linked list and maintain a min-heap of size K+1 to kee...read more
Top Nagarro SDE interview questions & answers
Popular interview questions of SDE
Reviews
Interviews
Salaries
Users/Month