Leetcode Remove Nth Node From End of List problem solution in Python programming

In the Leetcode Remove Nth Node From End of List problem solution in Python programming 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 in Python programming

class Solution:
    def removeNthFromEnd(self, head, n):
        temp = head
        c=1
        while temp.next is not None:
            temp = temp.next
            c+=1
        if c==1:
            head = None
            return head
        if c==n:
            head = head.next
            return head
        c = c-n
        temp = head
        while c!=1:
            temp = temp.next
            c-=1
        if temp.next.next == None:
            temp.next = None
        else:
            temp.next = temp.next.next
        return head

Also read,

By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.

Leave a Reply

Your email address will not be published. Required fields are marked *