



For the above-linked list, if k=2, then the value at the kth i.e second node from the end is ‘12’.
1.You don’t need to take any input. It has already been taken care of. Just implement the given function and return a pointer pointing to the k-th element from the last of the linked list.
2.It is guaranteed that k<=size of the linked list.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘2*T’ lines represent the ‘T’ test cases.
The first line of each test case contains integers denoting the nodes of the linked list. Each line is guaranteed to have -1 at the end to signify the end of the linked list.The second line of each test case contains an integer ‘k’
For each test case, return a pointer pointing to the node
which is at the k-th position from the last of the linked list.
1 <= T <= 50
0 <= N <= 10^5
-10^9 <= data <= 10^9
1 <=k <=N
node->data ≠ -1.
Where ‘T’ is the total number of test cases, ‘N’ denotes the number of elements in each Linked List, and ‘data‘ is the value of any node of the linked list and ‘k’ is the position of the element from the end to be returned.
Time Limit : 1 sec
Algorithm
Let us understand the above approach for the below-linked list for k=3.
Step 1: Initialize back and front to head.
*back=5 , *front=5.
Step 2: Move front three(k) steps ahead.
*front=6.
Step 3: Move both pointers one by one until ‘front’ reaches the end.
*back=23 ,*front=43
*back=7 , *front=76
*back=6 , *front=89
*back=43 , front=end
Step 4: ‘back’ points to the kth node from last. Therefore answer is 43.