All problems
Nth Node from End
Problem
Return the value of the n-th node from the end of a singly linked list.
n is 1-based, so n = 1 is the last node. Use the two-pointer trick: advance a lead pointer n steps, then move both pointers until the lead reaches the end.
Return None when n is out of range.
Hint
The n-th node from the end sits at index length - n.
Test cases
case 1
in: [[1,2,3,4,5],2]
expected: 4
case 2
in: [[1],1]
expected: 1
case 3
in: [[1,2],3]
expected: null
case 4
in: [[],1]
expected: null
Starter codePython
def nth_from_end(values, n):
# Your code here
pass4 test cases · run your code against them in the editor