Remove Nth Node From End Of The Linked List

Problem Statement

Given a singly linked list along with a positive integer N. Write a function to delete the Nth node from the end of the given linked list.

Problem Statement Understanding

Let's understand the given problem with the help of some examples:

If our initial linked list:

It means that here we had to remove the N = 3rd node from the end which is 3, so we will remove it.

After removing the 3rd node from the end of the linked list, our output resultant linked list is

If our initial linked list:

It means that here we had to remove the N=1st node from the end which is 6, so we will remove it.

After removing the 1st node from the end of the linked list, our output resultant linked list is:

I hope that now you have understood this problem. Now, think of how we can approach this problem?

One simple approach will be to first find the length of linked list and then using it find the Nth node from the end. Let’s see how we will do this.

If length of our linked list is P, it means that we have to find (P-N)th node from the beginning of the linked list and this node will be the Nth node from the end of the linked list.

So to delete Nth node using the above approach we will have to first traverse the linked list and find the length P of the linked list. Then we will have to again traverse the linked list (P-N+1) steps to reach the Nth node from the end.

This will work fine, but it is taking 2 traversals of linked list to find the Nth node from the end. Now try to think, do we actually require the length of linked list to find the Nth node from the end of the linked list?

So in approach we will see how we can do it in single traversal of linked list.

Approach

Our approach would be simple.
To remove the Nth node from the end, we need two things, which are:
1) the Nth node from the end and
2) the node just before it.

First, we need to find the Nth node from the end of the linked list. There are many ways to do so. We will use the most efficient one, i.e., using 2 pointers.

We start with 2 pointers from the head.

  • Move one of them to the Nth node.
  • Now, both pointers are N nodes apart. If we move them forward together, that distance will be maintained.
  • Now, move both the pointers together till the forward pointer points to the last node.
  • Now, the backward pointer is N nodes far from the forward node. So, it will point to the nth node from the end. That is our required node to delete, so we delete it.

Think of some cases that we have to take care of while performing this operation, also try to implement the approach yourself.

Below is the step-by-step algorithm for the above approach.

Algorithm

  • Initialize 2 pointers i and j pointing to head node.
  • Move the pointer j, N steps ahead.
  • If we reach the end before reaching the Nth node, this means N is greater than the length of the linked list (Nth node doesn’t exist). So, we simply stop and return.
  • Else declare a prev pointer to keep track of the node just before the node pointed by i.
  • Now move both the pointers, i and j, together(along with the prev pointe) till j reaches the end node.
  • Now, i will be pointing to the Nth node from the end.
  • Now we have to remove it from the linked list.
  • First, we simply disconnect the ith node from the linked list by doing prev->next = i->next.
  • If the node to remove is the first node, we need to modify the head pointer as well.
  • At the end, delete i.

Dry Run

Code Implementation:



#include
using namespace std;

struct Node {
    int val;
    Node* next;

    Node(int value){
        val = value;
        next = NULL;
    }
};

void push_front(Node** head, int new_val){
    Node* new_node = new Node(new_val);
    new_node->next = *head;
    *head = new_node;
}

void printList(Node* head){
    Node* i = head;
    while(i){
        cout<val<<" ";
        i = i->next;
    }
    cout<<"\n";
}

void remove_nth_node_from_end(Node** head, int n){
    Node *i, *j;
    i = j = *head;

    n--;
    // move j n steps ahead
    while(n>0 && j->next!=NULL){
        j = j->next;
        n--;
    }

    if(n!=0){
        cout<<"n is either <= 0 or larger than the linked list size\n";
        return;
    }

    // move i and j together till j reaches the end
    Node *prev = i;
    while(j->next!=NULL){
        prev = i;
        i = i->next;
        j = j->next;
    }

    // now i will point to the nth node from end
    // remove it
    prev->next = i->next;

    // if i points to first node
    // modify the head pointer as well
    if(i == *head){
        *head = i->next;
    }
    delete i;
}


int main(){
    Node* head = NULL;

    push_front(&head, 5);
    push_front(&head, 4);
    push_front(&head, 3);
    push_front(&head, 2);
    push_front(&head, 1);

    printList(head);
    // 1 2 3 4 5
    
    // to remove 3rd node from end
    remove_nth_node_from_end(&head, 3);

    printList(head);
    // 1 2 4 5
}

Output

1 2 3 4 5
1 2 4 5

Space complexity: O(1), as we are not using any extra space here.

Here N is the total number of nodes in the given linked list.

Through this article, we learned how to remove the nth node from the end of a linked list. Problems like these are good for strengthening your concepts in LinkedList. I would highly recommend you to practice more such problems from Linked List.

Previous post Circular Linked List – Insertion
Next post Remove all occurrences of duplicates from a sorted linked list.

Leave a Reply

Your email address will not be published. Required fields are marked *