Skip to content

Instantly share code, notes, and snippets.

@ericness
Created March 26, 2022 03:12
Show Gist options
  • Save ericness/7886a79b5ddaa8e1b5d36df0cd541a2c to your computer and use it in GitHub Desktop.
Save ericness/7886a79b5ddaa8e1b5d36df0cd541a2c to your computer and use it in GitHub Desktop.
LeetCode 19 One Pass Solution
from typing import Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
"""Remove the nth from the last node.
Args:
head (Optional[ListNode]): Head of list
n (int): Node to remove
Returns:
Optional[ListNode]: Modified list
"""
length = 0
current = head
nodes = {}
while current:
nodes[length] = current
current = current.next
length += 1
node_to_remove = length - n
current = nodes[max(node_to_remove - 1, 0)]
if current == head and node_to_remove == 0:
head = current.next
elif current.next:
current.next = current.next.next
else:
current.next = None
return head
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment