# 19. Remove Nth Node From End of List

## Description

See: <https://leetcode.com/problems/remove-nth-node-from-end-of-list/>

## Solution

I used the [runner technique](/cs-theory-and-problems/data-structures/linked-list.md#runner) to solve this problem.  I move the runner forward `n` times. Then I move both the walker and runner together at the same time. When the runner gets to the end of the linked list, the walker will be at the node prior to the node to be removed. Then it is a matter of updating the walker's next pointer.

My first version was messier than this. I had one while loop and a counter I incremented in it. If the counter was greater than `n` then I would move the walker. It worked, but this is tidier.

```python
class Solution:
    def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
        walker = runner = head
        
        # move the runner forward the n spots. This will leave a gap between the walker
        # and runner of the desired "back n elements"
        for i in range(n):
            runner = runner.next
        
        # Handles the case where n is the same size as the linked list. The runner reaches 
        # the end of the linked list, so just return the head's next item.
        if not runner:
            return head.next
        
        # Now move runner through the rest of the linked list, walker moves along too
        while runner.next:
            runner = runner.next
            walker = walker.next
        
        # Remove the "nth node from the end of the list" by updating the prior node's
        # next pointer                
        walker.next = walker.next.next
        return head
        
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://monica-granbois.gitbook.io/cs-theory-and-problems/problems/leetcode/19.-remove-nth-node-from-end-of-list.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
