# 19. Remove Nth Node From End of List

## The two-pass solution

`# Definition for singly-linked list.# class ListNode:#     def __init__(self, val=0, next=None):#         self.val = val#         self.next = nextclass Solution:    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:        dummy = ListNode(-1)        dummy.next = head        node = dummy        N = 0        while node:            N += 1            node = node.next        node = dummy        for _ in range(N-n-1):            node = node.next        node.next = node.next.next        return dummy.next`

## The one-pass solution

`# Definition for singly-linked list.# class ListNode:#     def __init__(self, val=0, next=None):#         self.val = val#         self.next = nextclass Solution:    def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode:        dummy = ListNode(-1)        dummy.next = head        first = second = dummy        for _ in range(n+1):first = first.next        while first:            first = first.next            second = second.next        second.next = second.next.next        return dummy.next`

# Reference

Data Scientist/MLE/SWE @takemobi

## More from Jimmy Shen

Data Scientist/MLE/SWE @takemobi