For our second problem, we’ll continue extending our
Write a method which returns the node that is
n nodes from the tail of the linked list.
0, we would return the tail node, if
1, we would return the second to last node, and so on.
# a -> b -> c -> d -> e linked_list.n_from_last(0) # 'e' node linked_list.n_from_last(3) # 'b' node
We’ll need to be creative in how we solve this problem since we only have a reference to the head node and not the tail!
.size() method on
LinkedList may be helpful.
- write a method in the
.n_from_last()takes one argument:
- the number of nodes counting from the tail.
- return the node instance at that location.
There are multiple ways we can solve this problem. Try to answer it on your own, and use the hint if you get stuck.