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:
Created Linked list is:
2 3 1 7
Linked List after Deletion is:
2 3 1



Input: 1 -> 2 -> 3 -> 4 -> NULL, N = 4
Output:
Created Linked list is:
1 2 3 4
Linked List after Deletion is:
2 3 4

Approach:

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

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include<bits/stdc++.h>
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)
    {
  
        // First pointer will point to
        // the head of the linked list
        Node *first = head;
  
        // Second pointer will poin 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 <= N
            if (second->next == NULL) 
            {
  
                // If count = N i.e.
                // delete the head node
                if (i == key - 1)
                    head = head->next;
                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
        first->next = first->next->next;
        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 << (tnode->data) << ( " ");
            tnode = tnode->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 << ("Created Linked list is:\n");
    llist->printList();
  
    int N = 1;
    llist->head = llist->deleteNode(N);
  
    cout << ("\nLinked List after Deletion is:\n");
    llist->printList();
}
  
// This code is contributed by Arnab Kundu 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 poin 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 <= N
            if (second.next == null) {
  
                // If count = N i.e. delete the head node
                if (i == key - 1)
                    head = head.next;
                return;
            }
            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
        first.next = first.next.next;
    }
  
    // Function to insert a new Node at front of the list
    public void push(int new_data)
    {
        Node new_node = new Node(new_data);
        new_node.next = head;
        head = new_node;
    }
  
    // Function to print the linked list
    public void printList()
    {
        Node tnode = head;
        while (tnode != null) {
            System.out.print(tnode.data + " ");
            tnode = tnode.next;
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        LinkedList llist = new LinkedList();
  
        llist.push(7);
        llist.push(1);
        llist.push(3);
        llist.push(2);
  
        System.out.println("\nCreated Linked list is:");
        llist.printList();
  
        int N = 1;
        llist.deleteNode(N);
  
        System.out.println("\nLinked List after Deletion is:");
        llist.printList();
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach
class Node: 
    def __init__(self, new_data): 
        self.data = new_data 
        self.next = None
class LinkedList:
    def __init__(self):
        self.head = None
  
    # createNode and and make linked list 
    def push(self, new_data): 
        new_node = Node(new_data) 
        new_node.next = self.head 
        self.head = new_node 
  
    def deleteNode(self, n):
        first = self.head
        second = self.head
        for i in range(n):
              
            # If count of nodes in the 
            # given list is less than 'n'
            if(second.next == None):
                  
                # If index = n then 
                # delete the head node
                if(i == n - 1):
                    self.head = self.head.next
                return self.head
            second = second.next
          
        while(second.next != None):
            second = second.next
            first = first.next
          
        first.next = first.next.next
      
    def printList(self):
        tmp_head = self.head
        while(tmp_head != None):
            print(tmp_head.data, end = ' ')
            tmp_head = tmp_head.next
          
# Driver Code
llist = LinkedList() 
llist.push(7
llist.push(1
llist.push(3
llist.push(2
print("Created Linked list is:")
llist.printList()
llist.deleteNode(1
print("\nLinked List after Deletion is:")
llist.printList()
  
# This code is contributed by RaviParkash

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
      
public class LinkedList
{
  
    // Head of list
    public Node head;
  
    // Linked list Node
    public class Node 
    {
        public int data;
        public Node next;
        public 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 poin 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 <= N
            if (second.next == null
            {
  
                // If count = N i.e. delete the head node
                if (i == key - 1)
                    head = head.next;
                return;
            }
            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
        first.next = first.next.next;
    }
  
    // Function to insert a new Node at front of the list
    public void push(int new_data)
    {
        Node new_node = new Node(new_data);
        new_node.next = head;
        head = new_node;
    }
  
    // Function to print the linked list
    public void printList()
    {
        Node tnode = head;
        while (tnode != null)
        {
            Console.Write(tnode.data + " ");
            tnode = tnode.next;
        }
    }
  
    // Driver code
    public static void Main(String[] args)
    {
        LinkedList llist = new LinkedList();
  
        llist.push(7);
        llist.push(1);
        llist.push(3);
        llist.push(2);
  
        Console.WriteLine("\nCreated Linked list is:");
        llist.printList();
  
        int N = 1;
        llist.deleteNode(N);
  
        Console.WriteLine("\nLinked List after Deletion is:");
        llist.printList();
    }
}
  
// This code is contributed by 29AjayKumar

chevron_right


Output:

Created Linked list is:
2 3 1 7 
Linked List after Deletion is:
2 3 1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.