Remove Nth Node From End Of List
Given a linked list, remove the n-th node from the end of list and return its head. Tags: Linked List
Try It!
Discussion
Video
Solution
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:
dummy = ListNode('dummy')
dummy.next = head
first = dummy
for i in range(n + 1):
first = first.next
second = dummy
while first:
first = first.next
second = second.next
second.next = second.next.next
return dummy.next