Skip to content

Commit

Permalink
re-solve "19. Remove Nth Node From End of List"
Browse files Browse the repository at this point in the history
  • Loading branch information
lancelote committed Nov 2, 2024
1 parent 3e232c4 commit 02927cf
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/remove_nth_node_from_end_of_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ def removeNthFromEnd(
self, head: ListNode | None, n: int
) -> ListNode | None:
dummy = ListNode(next=head)
slow, fast = dummy, dummy
slow = fast = dummy

for _ in range(n):
assert fast.next

fast = fast.next

while fast.next:
slow = slow.next # type: ignore
assert fast.next and slow.next

fast = fast.next
slow = slow.next

slow.next = slow.next.next # type: ignore
assert slow.next
slow.next = slow.next.next
return dummy.next

0 comments on commit 02927cf

Please sign in to comment.