Related Articles
Delete Nth node from the end of the given linked list
• Difficulty Level : Medium
• Last Updated : 06 Jul, 2020

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
2 3 1

Input: 1 -> 2 -> 3 -> 4 -> NULL, N = 4
Output:
1 2 3 4
2 3 4

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// 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)``    ``{`` ` `        ``// 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 <= 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 `

## Java

 `// 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 <= 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();``    ``}``}`

## Python3

 `# 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`

## C#

 `// 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 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 <= 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`
Output:
```Created Linked list is:
2 3 1 7