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->5
Input : 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 the Bth node from last is basically the same as deleting (length-B+1) from the start. In our approach, first, we evaluate the length of the 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 the next node of the deleted node.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
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;
}
Node* removeNthFromEnd(Node* head, int B)
{
int len = 0;
Node* tmp = head;
while (tmp != NULL) {
len++;
tmp = tmp->next;
}
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;
}
else if (B == len) {
return head->next;
}
else
{
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;
}
}
void display( struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
cout << temp->data << " " ;
temp = temp->next;
}
cout << endl;
}
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" ;
display(head);
head = removeNthFromEnd(head, 2);
cout << "Linked list after modification: \n" ;
display(head);
return 0;
}
|
C
#include<stdio.h>
#include<stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* create( struct Node* head, int x)
{
struct Node *temp, *ptr = head;
temp = ( struct Node*) malloc ( sizeof ( struct 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;
}
struct Node* removeNthFromEnd( struct Node* head, int B)
{
int len = 0;
struct Node* tmp = head;
while (tmp != NULL) {
len++;
tmp = tmp->next;
}
if (B > len)
{
printf ( "Length of the linked list is %d" ,len );
printf ( " we can't remove %dth node from the" ,B);
printf ( " linked list\n" );
return head;
}
else if (B == len) {
return head->next;
}
else
{
int diff = len - B;
struct Node* prev= NULL;
struct Node* curr = head;
for ( int i = 0;i < diff;i++){
prev = curr;
curr = curr->next;
}
prev->next = curr->next;
return head;
}
}
void display( struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
printf ( "%d " ,temp->data);
temp = temp->next;
}
printf ( "\n" );
}
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;
printf ( "Linked list before modification: \n" );
display(head);
head = removeNthFromEnd(head, 2);
printf ( "Linked list after modification: \n" );
display(head);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
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;
}
static Node removeNthFromEnd(Node head, int B)
{
int len = 0 ;
Node tmp = head;
while (tmp != null )
{
len++;
tmp = tmp.next;
}
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;
}
else if (B == len)
{
return head.next;
}
else
{
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;
}
}
static void display(Node head)
{
Node temp = head;
while (temp != null )
{
System.out.print(temp.data + " " );
temp = temp.next;
}
System.out.println();
}
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" );
display(head);
head = removeNthFromEnd(head, 2 );
System.out.print( "Linked list after modification: \n" );
display(head);
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def create( self , x):
new_node = Node(x)
if self .head is None :
self .head = new_node
return
last = self .head
while last. next :
last = last. next
last. next = new_node
def display( self ):
temp = self .head
while temp:
print (temp.data, end = " " )
temp = temp. next
def removeNthFromEnd(head, k):
first = head
second = head
count = 1
while count < = k:
second = second. next
count + = 1
if second is None :
head.value = head. next .value
head. next = head. next . next
return
while second. next is not None :
first = first. next
second = second. next
first. next = first. next . next
val = [ 1 , 2 , 3 , 4 , 5 ]
k = 2
ll = LinkedList()
for i in val:
ll.create(i)
print ( "Linked list before modification:" );
ll.display()
removeNthFromEnd(ll.head, k)
print ( "\nLinked list after modification:" );
ll.display()
|
C#
using System;
class GFG
{
class Node
{
public int data;
public Node next;
};
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;
}
static Node removeNthFromEnd(Node head, int B)
{
int len = 0;
Node tmp = head;
while (tmp != null )
{
len++;
tmp = tmp.next;
}
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;
}
else if (B == len)
{
return head.next;
}
else
{
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;
}
}
static void display(Node head)
{
Node temp = head;
while (temp != null )
{
Console.Write(temp.data + " " );
temp = temp.next;
}
Console.Write( "\n" );
}
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" );
display(head);
head = removeNthFromEnd(head, 2);
Console.Write( "Linked list after modification: \n" );
display(head);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function create(head , x) {
var 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 removeNthFromEnd(head , B) {
var len = 0;
var tmp = head;
while (tmp != null ) {
len++;
tmp = tmp.next;
}
if (B > len) {
document.write( "Length of the linked list is " + len);
document.write( " we can't remove " + B + "th node from the" );
document.write( " linked list\n" );
return head;
}
else if (B == len) {
return head.next;
}
else {
var diff = len - B;
var prev = null ;
var curr = head;
for (i = 0; i < diff; i++) {
prev = curr;
curr = curr.next;
}
prev.next = curr.next;
return head;
}
}
function display(head) {
var temp = head;
while (temp != null ) {
document.write(temp.data + " " );
temp = temp.next;
}
document.write( "<br/>" );
}
var head = null ;
head = create(head, 1);
head = create(head, 2);
head = create(head, 3);
head = create(head, 4);
head = create(head, 5);
var n = 2;
document.write( "Linked list before modification: <br/>" );
display(head);
head = removeNthFromEnd(head, 2);
document.write( "Linked list after modification: <br/>" );
display(head);
</script>
|
Output
Linked list before modification:
1 2 3 4 5
Linked list after modification:
1 2 3 5
Time Complexity: O(N)
Auxiliary Space: O(N)
Another Approach:
Two Pointer Approach
Deleting the Bth node from last is basically the same as deleting (length-B+1) from the start. In our approach, we will define 2 pointers, fast pointer and slow pointer.
Algorithm:
- Take two Node slowPtr and fastPtr, such that next points to the head
- Take one Node to store the head, initially it’s a dummy node(start), and the next of the node will be pointing to the head. The dummy node is taken to handle the edge case where B=N(size of the LinkedList)
- Start traversing until the fast pointer reaches the nth node
- Start traversing by one step both of the pointers until the fast pointers reach the end
- When the traversal is done, delete the next node to slowPtr
- Return the next of start
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
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;
}
Node* removeNthFromEnd(Node* head, int B)
{
Node *start = new Node();
start -> next = head;
Node* fastPtr = start;
Node* slowPtr = start;
for ( int i = 0; i < B; i++){
fastPtr = fastPtr->next;
}
while (fastPtr->next != NULL)
{
fastPtr = fastPtr->next;
slowPtr = slowPtr->next;
}
slowPtr->next = slowPtr->next->next;
return start->next;
}
void display( struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
cout << temp->data << " " ;
temp = temp->next;
}
cout << endl;
}
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" ;
display(head);
head = removeNthFromEnd(head, 2);
cout << "Linked list after modification: \n" ;
display(head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* create(Node* head, int x)
{
Node* ptr = head;
Node* temp = (Node*) malloc ( sizeof (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;
}
Node* removeNthFromEnd(Node* head, int B)
{
Node* start = (Node*) malloc ( sizeof (Node));
start->next = head;
Node* fastPtr = start;
Node* slowPtr = start;
for ( int i = 0; i < B; i++)
fastPtr = fastPtr->next;
while (fastPtr->next != NULL) {
fastPtr = fastPtr->next;
slowPtr = slowPtr->next;
}
slowPtr->next = slowPtr->next->next;
return start->next;
}
void display( struct Node* head)
{
struct Node* temp = head;
while (temp != NULL) {
printf ( "%d " , temp->data);
temp = temp->next;
}
printf ( "\n" );
}
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;
printf ( "Linked list before modification: \n" );
display(head);
head = removeNthFromEnd(head, 2);
printf ( "Linked list after modification: \n" );
display(head);
return 0;
}
|
Java
import java.io.*;
class GFG {
class Node {
int data;
Node next;
}
public Node create(Node head, int x)
{
Node temp = new Node();
Node ptr = head;
temp.data = x;
temp.next = null ;
if (head == null ) {
head = temp;
}
else {
while (ptr.next != null ) {
ptr = ptr.next;
}
ptr.next = temp;
}
return head;
}
public Node removeNthFromEnd(Node head, int B)
{
Node start = new Node();
start.next = head;
Node fastPtr = start;
Node slowPtr = start;
for ( int i = 0 ; i < B; i++) {
fastPtr = fastPtr.next;
}
while (fastPtr.next != null ) {
fastPtr = fastPtr.next;
slowPtr = slowPtr.next;
}
slowPtr.next = slowPtr.next.next;
return start.next;
}
public void display(Node head)
{
Node temp = head;
while (temp != null ) {
System.out.print(temp.data + " " );
temp = temp.next;
}
System.out.println();
}
public static void main(String[] args)
{
GFG l = new GFG();
Node head = null ;
head = l.create(head, 1 );
head = l.create(head, 2 );
head = l.create(head, 3 );
head = l.create(head, 4 );
head = l.create(head, 5 );
System.out.println(
"Linked List before modification: " );
l.display(head);
head = l.removeNthFromEnd(head, 2 );
System.out.println(
"Linked List after modification: " );
l.display(head);
}
}
|
Python3
class Node:
def __init__( self , key):
self .data = key
self . next = None
def create(head, x):
temp = Node(x)
ptr = head
if (head is None ):
head = temp
else :
while (ptr. next is not None ):
ptr = ptr. next
ptr. next = temp
return head
def removeNthFromEnd(head, B):
start = Node( - 1 )
start. next = head
fastPtr = start
slowPtr = start
for i in range (B):
fastPtr = fastPtr. next
while (fastPtr. next is not None ):
fastPtr = fastPtr. next
slowPtr = slowPtr. next
slowPtr. next = slowPtr. next . next
return start. next
def display(head):
temp = head
while (temp is not None ):
print (temp.data, end = " " )
temp = temp. next
head = None
head = create(head, 1 )
head = create(head, 2 )
head = create(head, 3 )
head = create(head, 4 )
head = create(head, 5 )
n = 2
print ( "Linked list before modification: " )
display(head)
head = removeNthFromEnd(head, 2 )
print ( "\nLinked list after modification: " )
display(head)
|
C#
using System;
public class GFG{
class Node {
public int data;
public Node next;
}
Node create(Node head, int x)
{
Node temp = new Node();
Node ptr = head;
temp.data = x;
temp.next = null ;
if (head == null ) {
head = temp;
}
else {
while (ptr.next != null ) {
ptr = ptr.next;
}
ptr.next = temp;
}
return head;
}
Node removeNthFromEnd(Node head, int B)
{
Node start = new Node();
start.next = head;
Node fastPtr = start;
Node slowPtr = start;
for ( int i = 0; i < B; i++) {
fastPtr = fastPtr.next;
}
while (fastPtr.next != null ) {
fastPtr = fastPtr.next;
slowPtr = slowPtr.next;
}
slowPtr.next = slowPtr.next.next;
return start.next;
}
void display(Node head)
{
Node temp = head;
while (temp != null ) {
Console.Write(temp.data + " " );
temp = temp.next;
}
Console.WriteLine();
}
static public void Main (){
GFG l = new GFG();
Node head = null ;
head = l.create(head, 1);
head = l.create(head, 2);
head = l.create(head, 3);
head = l.create(head, 4);
head = l.create(head, 5);
Console.WriteLine(
"Linked List before modification: " );
l.display(head);
head = l.removeNthFromEnd(head, 2);
Console.WriteLine(
"Linked List after modification: " );
l.display(head);
}
}
|
Javascript
class Node {
constructor() {
this .data;
this .next;
}
}
function create(head, x) {
let temp = new Node();
let ptr = head;
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 removeNthFromEnd(head, B) {
let start = new Node();
start.next = head;
let fastPtr = start;
let slowPtr = start;
for (let i = 0; i < B; i++) {
fastPtr = fastPtr.next;
}
while (fastPtr.next != null ) {
fastPtr = fastPtr.next;
slowPtr = slowPtr.next;
}
slowPtr.next = slowPtr.next.next;
return start.next;
}
function display(head) {
let temp = head;
while (temp != null ) {
process.stdout.write(temp.data + " " );
temp = temp.next;
}
process.stdout.write( "\n" );
}
let head = null ;
head = create(head, 1);
head = create(head, 2);
head = create(head, 3);
head = create(head, 4);
head = create(head, 5);
console.log( "Linked List before modification: " );
display(head);
head = removeNthFromEnd(head, 2);
console.log( "Linked List after modification: " );
display(head);
|
Output
Linked list before modification:
1 2 3 4 5
Linked list after modification:
1 2 3 5
Time complexity: O(N) where N is no of nodes in linked list
Space complexity: O(1) because using constant variables
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Oct, 2023
Like Article
Save Article