Given a singly linked list of size N, and an integer K. You need to swap the Kth node from beginning and Kth node from end in linked list.
Note: You need to swap the nodes through the links and not changing the content of the nodes.
First line of input contains the number of testcases T. The first line of every testacase contains N, number of nodes in linked list and K, the nodes to be swapped and the second line of contains the elements of the linked list.
For each testcase, if the nodes are swapped correctly, the output will be 1, else 0.
The task is to complete the function swapkthnode(), which has arguments head, num(no of nodes) and K, and it should return new head. The validation is done internally by the driver code to ensure that the swapping is done by changing references/pointers only. A correct code would always cause output as 1.
1 <= T <= 100
1 <= N <= 103
1 <= K <= 103
1 2 3 4
1 2 3 4 5
1 2 3 4
Testcase1: Here K = 1, hence after swapping the 1st node from beginning and end the new list will be 4 2 3 1.
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.