> For the complete documentation index, see [llms.txt](https://maksimdan.gitbook.io/interview-practice-problems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/369-plus-one-linked-list.md).

# 369 Plus One Linked List

Given a non-negative integer represented as **non-empty** a singly linked list of digits, plus one to the integer.

You may assume the integer do not contain any leading zero, except the number 0 itself.

The digits are stored such that the most significant digit is at the head of the list.

**Example:**

```
Input:
1->2->3

Output:
1->2->4
```

**The Idea:** A recursive solution to this is natural because we get a natural stack in the process. First iterate to the end. Then if the number is 9, then we know it must increment the next available base, so set it to 0. Otherwise if the number isn't 9, then we just increment the number and finish there with a flag. The final edge case to consider is when we finish iteration but no flag was triggered. This means value was zeroed out, and we must add a new`ListNode(1)` as the new head.

**Complexity:** O(N) time and space

```python
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    stop = False
    def plusOne(self, head):
        self.__plusOne(head)
        if self.stop is False:
            newHead = ListNode(1)
            newHead.next = head
            return newHead
        else:
            return head

    def __plusOne(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """

        if head is not None:
            self.plusOne(head.next)
            if head.val == 9 and self.stop is False:
                head.val = 0
            elif head.val != 9 and self.stop is False:
                head.val += 1
                self.stop = True
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/369-plus-one-linked-list.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
