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.
All elements are distinct.
A doubly linked list is a type of linked list that is bidirectional, that is, it can be traversed in both directions, forward and backward.
The first line of input contains T, the number of test cases.
The first line of each test case contains an integer K, as specified in the problem statement.
The second line contains the elements of the doubly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
For each test case print in a new line the sorted linked list, the elements of the sorted list should be single-space separated, terminated by -1.
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10000
1 <= K < N
Time Limit: 1 sec
We will use Insertion sort to sort the doubly linked list efficiently.
Iterate the given doubly linked list head till the last node n, where n is the size of the doubly linked list.
Let’s understand this in a detailed way.
Let us understand the working of the above algorithm with the help of an example, where N(Number of nodes in the doubly linked list) = 4, K = 2, and the doubly linked list is:
NULL←5←→6←→2←→3→NULL
Max Prefix
Merge Two Sorted Arrays Without Extra Space
Merge Two Sorted Arrays Without Extra Space
Delete Nodes On Regular Intervals
Add One To Linked List
Sort 0s, 1s, 2s