Find xth Node from End of Linked List

Medium

Given a linked list, find the xth node from the end.

Example
Linked list: 1→2→3→4
x: 2
Result: 3

Testing

Input Format

The first line contains an integer ‘T’ denoting the number of independent test cases.

For each test case the input has three lines:

  • An integer ‘n’ denoting the length of the linked list.
  • n space-separated integers denoting elements of the linked list.
  • An integer ‘x’ denoting the xth element from the end to be found.

Output Format

For each test case, an integer denoting the element to be found.

Sample Input

2
3
3 4 5
2
3
1 2 3
1

Expected Output

4
3

Constraints

1 <= T <= 100

1 <= x <= n <= 104

1 <= element <= 1000

Editorial Link: Editorial