Remove Nth node from end of the Linked List
Given a linked list. The task is to remove the Nth node from the end of the linked list.
Examples:
Input : 1->2->3->4->5 , N = 2
Output : 1->2->3->5Input : 7->8->4->3->2 , N = 1
Output : 7->8->4->3
Prerequisites:
1. Delete a node from the linked list.
2. Find the nth node from the end of the linked list
Approach:
Deleting Bth node from last is basically same as deleting (length-B+1) from start. In our approach first we evaluate the length of linked-list, then check
- If length < B, then we can’t remove the node
- If length = B, then return head->next
- If length > B, then it means we have to delete the intermediate node, we will delete this node and make its prev node point to next node of deleted node.
C++
// CPP program to delete nth node from last #include <bits/stdc++.h> using namespace std; // Structure of node struct Node { int data; struct Node* next; }; // Function to insert node in a linked list struct Node* create(struct Node* head, int x) { struct Node *temp, *ptr = head; temp = new Node(); temp->data = x; temp->next = NULL; if (head == NULL) head = temp; else { while (ptr->next != NULL) { ptr = ptr->next; } ptr->next = temp; } return head; } // Function to remove nth node from last Node* removeNthFromEnd(Node* head, int B) { // To store length of the linked list int len = 0; Node* tmp = head; while (tmp != NULL) { len++; tmp = tmp->next; } // B > length, then we can't remove node if (B > len) { cout << "Length of the linked list is " << len; cout << " we can't remove "<< B << "th node from the"; cout << " linked list\n"; return head; } // We need to remove head node else if (B == len) { // Return head->next return head->next; } else { // Remove len - B th node from starting int diff = len - B; Node* prev= NULL; Node* curr = head; for(int i = 0;i < diff;i++){ prev = curr; curr = curr->next; } prev->next = curr->next; return head; } } // This function prints contents of linked // list starting from the given node void dispaly(struct Node* head) { struct Node* temp = head; while (temp != NULL) { cout << temp->data << " "; temp = temp->next; } cout << endl; } // Driver code int main() { struct Node* head = NULL; head = create(head, 1); head = create(head, 2); head = create(head, 3); head = create(head, 4); head = create(head, 5); int n = 2; cout << "Linked list before modification: \n"; dispaly(head); head = removeNthFromEnd(head, 2); cout << "Linked list after modification: \n"; dispaly(head); return 0; } |
Java
// Java program to delete nth node from last class GFG { // Structure of node static class Node { int data; Node next; }; // Function to insert node in a linked list static Node create(Node head, int x) { Node temp, ptr = head; temp = new Node(); temp.data = x; temp.next = null; if (head == null) head = temp; else { while (ptr.next != null) { ptr = ptr.next; } ptr.next = temp; } return head; } // Function to remove nth node from last static Node removeNthFromEnd(Node head, int B) { // To store length of the linked list int len = 0; Node tmp = head; while (tmp != null) { len++; tmp = tmp.next; } // B > length, then we can't remove node if (B > len) { System.out.print("Length of the linked list is " + len); System.out.print(" we can't remove "+ B + "th node from the"); System.out.print(" linked list\n"); return head; } // We need to remove head node else if (B == len) { // Return head.next return head.next; } else { // Remove len - B th node from starting int diff = len - B; Node prev= null; Node curr = head; for(int i = 0; i < diff; i++) { prev = curr; curr = curr.next; } prev.next = curr.next; return head; } } // This function prints contents of linked // list starting from the given node static void dispaly(Node head) { Node temp = head; while (temp != null) { System.out.print(temp.data + " "); temp = temp.next; } System.out.println(); } // Driver code public static void main(String[] args) { Node head = null; head = create(head, 1); head = create(head, 2); head = create(head, 3); head = create(head, 4); head = create(head, 5); int n = 2; System.out.print("Linked list before modification: \n"); dispaly(head); head = removeNthFromEnd(head, 2); System.out.print("Linked list after modification: \n"); dispaly(head); } } // This code is contributed by Rajput-Ji |
C#
// C# program to delete nth node from last using System; class GFG { // Structure of node class Node { public int data; public Node next; }; // Function to insert node in a linked list static Node create(Node head, int x) { Node temp, ptr = head; temp = new Node(); temp.data = x; temp.next = null; if (head == null) head = temp; else { while (ptr.next != null) { ptr = ptr.next; } ptr.next = temp; } return head; } // Function to remove nth node from last static Node removeNthFromEnd(Node head, int B) { // To store length of the linked list int len = 0; Node tmp = head; while (tmp != null) { len++; tmp = tmp.next; } // B > length, then we can't remove node if (B > len) { Console.Write("Length of the linked list is " + len); Console.Write(" we can't remove " + B + "th node from the"); Console.Write(" linked list\n"); return head; } // We need to remove head node else if (B == len) { // Return head.next return head.next; } else { // Remove len - B th node from starting int diff = len - B; Node prev= null; Node curr = head; for(int i = 0; i < diff; i++) { prev = curr; curr = curr.next; } prev.next = curr.next; return head; } } // This function prints contents of linked // list starting from the given node static void dispaly(Node head) { Node temp = head; while (temp != null) { Console.Write(temp.data + " "); temp = temp.next; } Console.Write("\n"); } // Driver code public static void Main(String[] args) { Node head = null; head = create(head, 1); head = create(head, 2); head = create(head, 3); head = create(head, 4); head = create(head, 5); Console.Write("Linked list before modification: \n"); dispaly(head); head = removeNthFromEnd(head, 2); Console.Write("Linked list after modification: \n"); dispaly(head); } } // This code is contributed by 29AjayKumar |
Output:
Linked list before modification: 1 2 3 4 5 Linked list after modification: 1 2 3 5
Recommended Posts:
- Remove every k-th node of the linked list
- Remove first node of the linked list
- Remove last node of the linked list
- Create new linked list from two given linked list with greater element at each node
- Swap Kth node from beginning with Kth node from end in a Linked List
- Linked List | Set 2 (Inserting a node)
- Linked List | Set 3 (Deleting a node)
- Squareroot(n)-th node in a Linked List
- Delete Nth node from the end of the given linked list
- Program for n'th node from the end of a Linked List
- Detect and Remove Loop in a Linked List
- Remove duplicates from a sorted linked list
- Remove duplicates from an unsorted linked list
- Sum of nodes in a linked list which are greater than next node
- Delete a node in a Doubly Linked List
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.



