> For the complete documentation index, see [llms.txt](https://monica-granbois.gitbook.io/cs-theory-and-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://monica-granbois.gitbook.io/cs-theory-and-problems/problems/leetcode/206.-reverse-linked-list.md).

# 206. Reverse Linked List

## Description

See: <https://leetcode.com/problems/reverse-linked-list/>

## Solution

Standard reverse a linked list question. Implemented both iteratively and recursively.

### Iterative

```python
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        node = head
        prev = None
        while node != None:
            next_node = node.next
            node.next = prev
            prev = node
            node = next_node
        return prev
```

Time complexity: O(n)

Space complexity: O(1)

### Recursive

```python
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        def helper(head, prev):
            if not head:
                return prev
            temp = head.next
            head.next = prev
            return helper(temp, head)
        return helper(head, None)
```

Time complexity: O(n)

Space complexity: O(n) - recursive stack is the size of the linked list
