forked from DengWangBao/Leetcode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusOneLinkedList.java
More file actions
27 lines (25 loc) · 696 Bytes
/
PlusOneLinkedList.java
File metadata and controls
27 lines (25 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import java.util.Stack;
public class PlusOneLinkedList {
public ListNode plusOne(ListNode head) {
if (head == null) {
return head;
}
Stack<ListNode> stack = new Stack<ListNode>();
for (ListNode node = head; node != null; node = node.next) {
stack.push(node);
}
int k = 1;
while (!stack.isEmpty()) {
ListNode node = stack.pop();
int val = node.val + k;
node.val = val % 10;
k = val / 10;
if (k == 0) {
return head;
}
}
ListNode node = new ListNode(k);
node.next = head;
return node;
}
}