19. Remove Nth Node From End of List
Medium
Given a linked list, remove the n-th node from the end of list and return its head.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
dummy = ListNode(-1)
dummy.next = head
node = head
l = 0
while(node!=None):
l += 1
node = node.next
l = l-n
node = dummy
while(l > 0):
l -= 1
node = node.next
node.next = node.next.next
return dummy.next