ID | Title | Difficulty | |
---|---|---|---|
Loading... |
369. Plus One Linked List
Medium
LeetCode
Linked List, Math
Problem
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:
Input: head = [1,2,3]
Output: [1,2,4]
Example 2:
Input: head = [0]
Output: [1]
Code
class Solution {
public ListNode plusOne(ListNode head) {
ListNode dummy = new ListNode(0);
dummy.next = head;
ListNode notNine = dummy;
while (head != null) {
if (head.val != 9) {
notNine = head;
}
head = head.next;
}
notNine.val++;
notNine = notNine.next;
while (notNine != null) {
notNine.val = 0;
notNine = notNine.next;
}
return dummy.val != 0 ? dummy : dummy.next;
}
}
按 <- 键看上一题!
368. Largest Divisible Subset
按 -> 键看下一题!
370. Range Addition