Timer is Running

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 the end in the linked list.
Note: You need to swap the nodes through the links and not changing the content of the nodes.

Example 1:

N = 4,  K = 1
value[] = {1,2,3,4}
Output: 1
Explanation: Here K = 1, hence after
swapping the 1st node from the beginning
and end thenew list will be 4 2 3 1.

Example 2:

N = 5,  K = 3
value[] = {1,2,3,4,5}
Output: 1
Explanation: Here k = 3, hence after
swapping the 3rd node from the beginning
and end the new list will be 1 2 3 4 5.

Your Task: 
The task is to complete the function swapkthnode(), which has arguments head, num(no of nodes), and K, and it should return a 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.

Expected Time Complexity: O(n)
Expected Auxillary space Complexity: O(1)

1 <= N <= 103
1 <= K <= 103


** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Mridul Gupta
Author: SIDG

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.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.