
Linked list :
Suppose K = 6
Initially, you will be at Node 1.
After first step -> Node 2
After second step -> Node 3
After third step -> Node 4
After fourth step -> Node 5
After fifth step -> Node 2
After sixth step -> Node 3
At the end, you will be at Node 3, so Node 3 is the answer.
The first line contains an integer 'T', which denotes the number of test cases to be run. Then the test cases follow.
The first line of each test case contains three space-separated integers, 'N', 'K', 'S', denoting the length of the given linked list, the number of steps, and starting index of the linked list cycle respectively.
The second line contains 'N' space-separated positive integers, which are the values present in nodes of the linked list.
Note: You will not get 'S' in the function, it is only to show how the linked list cycle is present.
For each test case, return the node where you will reach after moving exactly 'K' steps.
1 <= T <= 10
2 <= N <= 10^5
1 <= K <= 10^9
0 <= S < N - 1
1 <= A[i] <= 10^9
It is guaranteed that the sum of 'N' <= 10 ^ 5 in all the test cases.
Time Limit: 1 sec
Start from the given head, and move exactly 'K' steps.
Find the starting element of the cycle and length of the cycle, now suppose if you are at the starting element of the cycle and the length of the cycle is 'l' and you have to move 'X' steps, so moving 'X' steps is the same as moving 'X' % 'l' steps, because every time you move 'l' steps you will come back to the same position, so we will eliminate as many multiples of 'l' as we can, and in the end, the remaining steps will be 'X' % 'l'. Where '%' stands for modulus.
Suppose we have take 'Y' number of steps to reach the starting point of the linked list cycle then the value of 'X' is min(0, 'K - X'). Hence after iterating till the starting point of the linked list cycle, we just need to iterate 'X % I' steps more.