remove_nth_node_from_end_of_list

 1from utils import ListNode
 2from typing import Optional
 3
 4
 5# @leet start
 6class Solution:
 7    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
 8        """
 9        To remove the nth node from the end, create a dummy pointer, and then
10        iterate the fast pointer n + 1 times to make it n + 1 positions faster
11        than the slow pointer. Finally, advance the slow and fast pointer in tandem,
12        and then when the fast pointer is None, we skip slow.next's pointer and
13        set it to slow.next.next.
14
15        Finally, we return the dummy pointer's next (the old head) to complete
16        the removal.
17        """
18        dummy = ListNode()
19        dummy.next = head
20        slow = dummy
21        fast = dummy
22        for _ in range(n + 1):
23            fast = fast.next
24        while fast:
25            slow = slow.next
26            fast = fast.next
27        slow.next = slow.next.next
28        return dummy.next
29
30
31# @leet end
32
33
34def test():
35    assert 2 + 2 == 4
class Solution:
 7class Solution:
 8    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
 9        """
10        To remove the nth node from the end, create a dummy pointer, and then
11        iterate the fast pointer n + 1 times to make it n + 1 positions faster
12        than the slow pointer. Finally, advance the slow and fast pointer in tandem,
13        and then when the fast pointer is None, we skip slow.next's pointer and
14        set it to slow.next.next.
15
16        Finally, we return the dummy pointer's next (the old head) to complete
17        the removal.
18        """
19        dummy = ListNode()
20        dummy.next = head
21        slow = dummy
22        fast = dummy
23        for _ in range(n + 1):
24            fast = fast.next
25        while fast:
26            slow = slow.next
27            fast = fast.next
28        slow.next = slow.next.next
29        return dummy.next
def removeNthFromEnd(self, head: Optional[utils.ListNode], n: int) -> Optional[utils.ListNode]:
 8    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
 9        """
10        To remove the nth node from the end, create a dummy pointer, and then
11        iterate the fast pointer n + 1 times to make it n + 1 positions faster
12        than the slow pointer. Finally, advance the slow and fast pointer in tandem,
13        and then when the fast pointer is None, we skip slow.next's pointer and
14        set it to slow.next.next.
15
16        Finally, we return the dummy pointer's next (the old head) to complete
17        the removal.
18        """
19        dummy = ListNode()
20        dummy.next = head
21        slow = dummy
22        fast = dummy
23        for _ in range(n + 1):
24            fast = fast.next
25        while fast:
26            slow = slow.next
27            fast = fast.next
28        slow.next = slow.next.next
29        return dummy.next

To remove the nth node from the end, create a dummy pointer, and then iterate the fast pointer n + 1 times to make it n + 1 positions faster than the slow pointer. Finally, advance the slow and fast pointer in tandem, and then when the fast pointer is None, we skip slow.next's pointer and set it to slow.next.next.

Finally, we return the dummy pointer's next (the old head) to complete the removal.

def test():
35def test():
36    assert 2 + 2 == 4