Sitemap
4 min readMay 14, 2025

--

LinkedList- Finding nth node from End.

Hello there Coders,

I am working my way through the top of the ladder of any Developer.

In that I am trying to get my head around the OG “DSA”

I am planning to write my learning curve and the stuffs I have learned and the problem I have solved in this space. Hoping to be helpful (but really, I just love writing).

So here we go for the classic Find the Nth node from the end of the Linked List.

Let’s say we have a linked list of 4 values.

Press enter or click to view image in full size

here if I want to find the 2nd Node from the end that is 3. What are the approaches I can take up?

There are several approaches let’s talk about the three I have used and let’s go from the brutal “Brute Force” approach.

/*
* Start iteration from the head of the node
* for each iteration calculate the number of remaining nodes in the list from the current node
* if the number of node is less than n-1 value return null;
* if the number of nodes is grater than n-1 value continue the iteration
* if the number of node is equal to the n-1 value return the node;
* time complexity: O(n^2)
* Space complexity: O(1)
*/
public static Node bruteForce(int n, LinkedList list){
Node temp = list.getHead();
Node current;
int secondaryLength=0;
while(secondaryLength!= n-1){
secondaryLength=0;
current = temp.getNext();
while(current != null){
secondaryLength++;
current= current.getNext();
}
if(secondaryLength<n-1){
System.out.println("Fewer");
return null;
}else if(secondaryLength>n-1){
temp = temp.getNext();
}
}
return temp;
}

As you can see here, we must iterate through the Linked list a humungous number of times as a loop within another loop.

So, what we do here is that we iterate through each node of the list and check for the number of nodes after them and compare that number with the n-1 value and based on that comparison we find out the result.

Get Devhub’s stories in your inbox

Join Medium for free to get updates from this writer.

All right let’s jump into the better approach.

/*
* More Efficient approach
* Calculate the length of the list as M
* Calculate new M with the formula M = (M-n)+1{Formula is used to identify the nth node from the beginning of the list
* Iterate through the list until new M and return the node
* time complexity: O(n)
* space complexity: O(1)
*/
public static Node betterVersion(int n, LinkedList list){
int newPosition=0;
Node temp = list.getHead();
while(temp != null){
temp = temp.getNext();
newPosition++;
}
if(n>newPosition){
return null;
}
newPosition= (newPosition-n)+1;
int grower=1;
temp = list.getHead();
while(newPosition!= grower){
temp = temp.getNext();
grower++;
}
return temp;
}

This one is more of a mathematical approach. For the given list and the given nth Node, we produce the value that represents the nth node from the beginning of the list. For that first we need the length of the list. Then based on the length of the list we produce the value that represent the nth node from the beginning of the list.

We use a Formula for the, M = (M+n)-1 where M being the length of the list.

Remember I called it mathematical approach.

Yet this is not the best approach we can come up with why?

Because we scan through the list twice in this approach.

We can improve this even more. HOW?

Let’s see

 /*
* The classic Two Pointer Approach
* Initiate Two Pointers fast,slow both pointing to the head node
* iterate the fast pointer for n times
* once the fast pointer has reach n iterations start iterating the slow pointer as well
* continue the simultaneous iteration until fast is equal to null
* return the slow pointer
*/
public static Node mostEfficientOrTwoPointer(int n, LinkedList list){
Node fast = list.getHead();
Node slow = list.getHead();
int moves =0;

while(fast != null){
fast = fast.getNext();
moves++;
if(moves>n){
slow=slow.getNext();
}
}
return slow;
}

So here we go, we got the OG Two Pointer.

Here we got two pointer: fast and slow(I gotta name my variables better sorry) they both point to the head at the beginning. The fast pointer fist moves n times ahead of the slow pointer by that we create a window that hold n nodes in them. So, when we move that window to the end of the list, we get the nth node of the list from the end.

Press enter or click to view image in full size

So yes, that’s all as easy as it is.

Peace out guys. You can reach out to me https://www.threads.com/@dev_hub2000.

--

--