369. Plus One Linked List

T: O(n)

S: O(1)

like 2816, so in the end(node == null)... add 1 is ok

```java
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode plusOne(ListNode head) {
        int t = dfs(head);
        ListNode cur = head;
        if (t != 0) {
            cur = new ListNode(t);
            cur.next = head;
        }
        return cur;
    }
    private int dfs(ListNode node) {
        if (node == null) {
            return 1;
        }
        int t = node.val + dfs(node.next);
        node.val = t%10;
        return t/10;
    }
}
```

Last updated