1->2->3->4->5->6, n=2 ==> 5
List
twice, one for finding the length and the other to return the position.
We can get the position of the Nth Node
from the end with the formula: 'length-n+1'.
1. First find the length of the List. Traverse the List
one more time to the Nth position
and return the Node
.
2. Since we didn't use extra memory, the runtime is O(n)
and space complexity is O(1)
public ListNode findNthNodeFromEnd(ListNode head, int n) { }
C
Java
Python