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,
- Leetcode Remove Nth Node From End of List problem solution in C++
- Leetcode Remove Nth Node From End of List problem solution in Java
- Leetcode Remove Nth Node From End of List problem solution in C
- Leetcode Remove Nth Node From End of List problem solution in C#