e of 'k,' then left-out nodes, in the end, should be reversed as well. Example : Given this linked list: 1 -> 2 -> 3 -> 4 -> 5 For k = 2, you should return: 2 -> 1 -> 4 -> 3 -> 5 For k = 3, you should return: 3 -> 2 -> 1 -> 5 -> 4 Note : No need to print the list, it has already been taken care. Only return the new head to the list. Input format : The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow. The first line of each test case or q
Given a singly linked list of integers, reverse the nodes of the linked list 'k' at a time and return its modified list.
'k' is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of 'k,' then left-out nodes, in the end, should be reversed as well.
Example :
Given this linked list: 1 -> 2 -> 3 -> 4 -> 5
For k = 2, you should return: 2 -> 1 -> 4 -> 3 -> 5
For k = 3, you should return: 3 -> 2 -> 1 -> 5 -> 4
Note :
No need to print the list, it has already been taken care. Only return the new head to the list.
Input format :
The first line contains an Integer 't' which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case or query contains the elements of the singly linked list separated by a single space.
The second line of input contains a single integer depicting the value of 'k'.
Remember/Consider :
While specifying the list elements for input, -1 indicates the end of the singly linked list and hence, would never be a list element
Output format :
For each test case/query, print the elements of the updated singly linked list.
Output for every test case will be printed in a separate line..
Step by step
Solved in 3 steps with 1 images