Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Remove Nth Node From End of List problem solution

YASH PAL, 31 July 2024

In this Leetcode Remove Nth Node From End of List problem solution we have given the head of a linked list, remove the nth node from the end of the list and return its head.

Leetcode Remove Nth Node From End of List problem solution

Problem solution in Python.

class Solution:
    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:        
        slow = fast = head
        
        for i in range(n):
            fast = fast.next 
            
        if not fast:
            return head.next 
        
        while fast and fast.next:
            slow = slow.next 
            fast = fast.next 
        
        slow.next = slow.next.next 
        return head

Problem solution in Java.

public ListNode removeNthFromEnd(ListNode head, int n) {
    if( head == null )
        return null;
    ListNode fakeHead = new ListNode(-1);
    fakeHead.next = head;
    
    ListNode prev = fakeHead, slow = head;
    ListNode fast = head;
    for(int i = 1; i < n; i++)
        fast = fast.next;
    while(fast.next != null) {
        fast = fast.next;
        prev = slow;
        slow = slow.next;
    }
    prev.next = slow.next;
    return fakeHead.next;
}

Problem solution in C++.

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {

        //checking basic  conditions
        if(head==NULL)return head;
        if(n==0)return head;

        ListNode* slow = head;
        ListNode* fast = head;
        ListNode* prev = NULL;

        while(n!=0){
            fast = fast->next;
            n--;
        }

        while(fast!=NULL){
            prev = slow;
            fast=fast->next;
            slow=slow->next;
        }

        if(prev==NULL){
            ListNode* newHead = slow->next;
            delete slow;
            return newHead;
        }

        prev->next = slow->next;
        delete slow;
        return head;
    }
};

Problem solution in C.

struct ListNode* removeNthFromEnd(struct ListNode* head, int n) 
{
    struct ListNode* current;
    current = head;
    int count = 0;
    
    while(current != NULL)
    {
        current = current->next;
        count++;
    }
    
    current = head;
    if(count==n)
        return current->next;
    
    count = count-n-1;
    while(count--)
    {
        current = current->next;
    }
    
    current->next = current->next->next;
    
    return head;
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes