-
Notifications
You must be signed in to change notification settings - Fork 4
/
AddTwoNumbersII.java
47 lines (41 loc) · 1.17 KB
/
AddTwoNumbersII.java
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.dbc.code;
import java.util.List;
import java.util.Stack;
public class AddTwoNumbersII {
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;
}
}
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Stack<ListNode> stack1 = new Stack<>();
Stack<ListNode> stack2 = new Stack<>();
while (l1 != null) {
stack1.add(l1);
l1 = l1.next;
}
while (l2 != null) {
stack2.add(l2);
l2 = l2.next;
}
ListNode res = null;
int remain = 0;
while (!stack1.isEmpty() || !stack2.isEmpty() || remain != 0) {
int num1 = stack1.isEmpty() ? 0 : stack1.pop().val;
int num2 = stack2.isEmpty() ? 0 : stack2.pop().val;
ListNode node = new ListNode((remain + num1 + num2) % 10);
remain = (remain + num1 + num2) / 10;
node.next = res;
res = node;
}
return res;
}
}