Delete Nth node from the end of the given linked list

• Difficulty Level : Medium
• Last Updated : 18 Nov, 2021

Given a linked list and an integer N, the task is to delete the Nth node from the end of the given linked list.

Examples:

Input: 2 -> 3 -> 1 -> 7 -> NULL, N = 1
Output:
2 3 1 7
The linked list after deletion is:
2 3 1

Input: 1 -> 2 -> 3 -> 4 -> NULL, N = 4
Output:
1 2 3 4
The linked list after deletion is:
2 3 4

Intuition:

Lets  K be the total nodes in the linked list.

Observation : The Nth node from the end is (K-N+1)th node from the beginning.

So the problem simplifies down to that we have to find  (K-N+1)th node from the beginning.

• One way of doing it is to find the length (K) of the linked list in one pass and then in the second pass move (K-N+1) step from the beginning to reach the Nth node from the end.
• To do it in one pass. Let’s take the first pointer and move N step from the beginning. Now the first pointer is (K-N+1) steps away from the last node, which is the same number of steps the second pointer require to move from the beginning to reach the Nth node from the end.

Approach:

• Take two pointers; the first will point to the head of the linked list and the second will point to the Nth node from the beginning.
• Now keep incrementing both the pointers by one at the same time until the second is pointing to the last node of the linked list.
• After the operations from the previous step, the first pointer should point to the Nth node from the end now. So, delete the node the first pointer is pointing to.

Below is the implementation of the above approach:

Javascript


Output
2 3 1 7
2 3 1

we already covered the iterative version above,
Now let us see its recursive approach as well,

Recursive Approach :
1) Create a dummy node and create a link from dummy node to head node. i.e, dummy->next = head
2) Then we will use the recursion stack to keep track of elements that are being pushed in recursion calls.
3) While popping the elements from recursion stack, we will decrement the N(position of target node from the end of linked list) i.e, N = N-1.
4) When we reach (N==0) that means we have reached at the target node,
5) But here is the catch, to delete the target node we require its previous node,
6) So we will now stop when (N==-1) i.e, we reached the previous node.
7) Now it is very simple to delete the node by using previousNode->next = previousNode->next->next.

C++

Output
1 2 3 4 5
1 2 3 5
1
1 2
1

Two Pointer Approach – Slow and Fast Pointers

This problem can be solved by using two pointer approach as below:

• Take two pointers – fast and slow. And initialize their values as head node
• Iterate fast pointer till the value of n.
• Now, start iteration of fast pointer till the None value of the linked list. Also, iterate slow pointer.
• Hence, once the fast pointer will reach to the end the slow pointer will reach the node which you want to delete.
• Replace the next node of the slow pointer with the next to next node of the slow pointer.

Python

Output
***** Linked List Before deletion *****
1
2
3
4
5
************** Delete nth Node from the End *****
*********** Linked List after Deletion *****
1
2
3
5

Time complexity: O(n)

My Personal Notes arrow_drop_up