Delete Nth node from the end of the given linked list
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:
The created linked list is:
2 3 1 7
The linked list after deletion is:
2 3 1Input: 1 -> 2 -> 3 -> 4 -> NULL, N = 4
Output:
The created linked list is:
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 Nthnode 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 Nthnode 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:
// C++ implementation of the approach
#include
using namespace std;
class LinkedList
{
public:
// Linked list Node
class Node
{
public:
int data;
Node* next;
Node[int d]
{
data = d;
next = NULL;
}
};
// Head of list
Node* head;
// Function to delete the nth node from
// the end of the given linked list
Node* deleteNode[int key]
{
// We will be using this pointer for holding
// address temporarily while we delete the node
Node *temp;
// First pointer will point to
// the head of the linked list
Node *first = head;
// Second pointer will point to the
// Nth node from the beginning
Node *second = head;
for [int i = 0; i < key; i++]
{
// If count of nodes in the given
// linked list is next == NULL]
{
// If count = N i.e.
// delete the head node
if [i == key - 1]{
temp = head;
head = head->next;
free [temp];
}
return head;
}
second = second->next;
}
// Increment both the pointers by one until
// second pointer reaches the end
while [second->next != NULL]
{
first = first->next;
second = second->next;
}
// First must be pointing to the
// Nth node from the end by now
// So, delete the node first is pointing to
temp = first->next;
first->next = first->next->next;
free [temp];
return head;
}
// Function to insert a new Node
// at front of the list
Node* push[int new_data]
{
Node* new_node = new Node[new_data];
new_node->next = head;
head = new_node;
return head;
}
// Function to print the linked list
void printList[]
{
Node* tnode = head;
while [tnode != NULL]
{
cout data] next;
}
}
};
// Driver code
int main[]
{
LinkedList* llist = new LinkedList[];
llist->head = llist->push[7];
llist->head = llist->push[1];
llist->head = llist->push[3];
llist->head = llist->push[2];
cout printList[];
int N = 1;
llist->head = llist->deleteNode[N];
cout printList[];
}
// This code is contributed by Arnab Kundu
|
// Java implementation of the approach
class LinkedList {
// Head of list
Node head;
// Linked list Node
class Node {
int data;
Node next;
Node[int d]
{
data = d;
next = null;
}
}
// Function to delete the nth node from
// the end of the given linked list
void deleteNode[int key]
{
// First pointer will point to
// the head of the linked list
Node first = head;
// Second pointer will point to the
// Nth node from the beginning
Node second = head;
for [int i = 0; i < key; i++] {
// If count of nodes in the given
// linked list is 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
Empty Linked List
1 2
1
Two Pointer Approach – Slow and Fast Pointers This problem can be solved by using two pointer approach as below:
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]
Article Tags :
Linked List
Delete a Linked List Traversal Practice Tags :
Linked List Traversal
Read Full Article
Remove Nth node from end of the Linked ListGiven a linked list. The task is to remove the Nth node from the end of the linked list. Examples:
Recommended: Please try your approach on {IDE} first, before moving on to the solution. Prerequisites:
C
C++
Output
Linked list before modification:
1 2 3 4 5
Linked list after modification:
1 2 3 5
Another Approach: Two Pointer Approach
C++
|