Nth-to-Last Node
In this lesson, we will learn how to get the Nth-to-Last Node from a given linked list.
We'll cover the following...
In this lesson, we are going to find how to get the Nth-to -Last Node from a linked list. First of all, we’ll clarify what we mean by Nth-to -Last Node in the illustration below:
As you can see from the illustration above, if N
equals 2
, we want to get the second to last node from the linked list.
We will be using two solutions to solve this problem.
Solution 1 #
We’ll break down this solution in two simple steps:
- Calculate the length of the linked list.
- Count down from the total length until
n
is reached.
For example, if we have a linked list of length four, then we’ll begin from the head node and decrement the calculated length of the linked ...