- categories: Code, Interview Question, leetcode, Medium
- source: https://leetcode.com/problems/remove-nth-node-from-end-of-list
- topics: Linked List
Given the head
of a linked list, remove the nth
node from the end of the list and return its head.
class Solution:
def removeNthFromEnd(self, head, n):
"""
:type head: ListNode
:type n: int
:rtype: ListNode
"""
dummy = ListNode(0, head)
first, second = dummy, dummy
# Advances first pointer so that the gap between first and second is n nodes apart
for i in range(n + 1):
first = first.next
# Move first to the end, maintaining the gap
while first is not None:
first = first.next
second = second.next
second.next = second.next.next
return dummy.next