# 369. Plus One Linked List

## Description

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

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

Example 1:

Output: [1,2,4]

Example 2:

Output: [1]

Constraints:

• The number of nodes in the linked list is in the range [1, 100].
• 0 <= Node.val <= 9
• The number represented by the linked list does not contain leading zeros except for the zero itself.

## Solutions

• /**
* 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 {
ListNode dummy = new ListNode(0, head);
ListNode target = dummy;
}
}
++target.val;
target = target.next;
while (target != null) {
target.val = 0;
target = target.next;
}
return dummy.val == 1 ? dummy : dummy.next;
}
}

• /**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode() : val(0), next(nullptr) {}
*     ListNode(int x) : val(x), next(nullptr) {}
*     ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* dummy = new ListNode(0, head);
ListNode* target = dummy;
}
++target->val;
target = target->next;
while (target) {
target->val = 0;
target = target->next;
}
return dummy->val == 1 ? dummy : dummy->next;
}
};

• # Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
def plusOne(self, head: ListNode) -> ListNode:
target = dummy
target.val += 1
target = target.next
while target:
target.val = 0
target = target.next
return dummy if dummy.val else dummy.next

• /**
* type ListNode struct {
*     Val int
*     Next *ListNode
* }
*/
target := dummy
}
}
target.Val++
target = target.Next
for target != nil {
target.Val = 0
target = target.Next
}
if dummy.Val == 1 {
return dummy
}
return dummy.Next
}