forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_445.java
116 lines (101 loc) · 3.58 KB
/
_445.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.fishercoder.solutions;
import com.fishercoder.common.classes.ListNode;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;
/**
* 445. Add Two Numbers II
*
* You are given two non-empty linked lists representing two non-negative integers.
* The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Follow up:
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.
Example:
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
*/
public class _445 {
public static class Solution1 {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Deque<Integer> stack1 = popIntoStack(l1);
Deque<Integer> stack2 = popIntoStack(l2);
int sum = 0;
ListNode list = new ListNode(0);
while (!stack1.isEmpty() || !stack2.isEmpty()) {
if (!stack1.isEmpty()) {
sum += stack1.removeFirst();
}
if (!stack2.isEmpty()) {
sum += stack2.removeFirst();
}
list.val = sum % 10;
ListNode head = new ListNode(sum / 10);
head.next = list;
list = head;
sum /= 10;
}
return list.val == 0 ? list.next : list;
}
private Deque<Integer> popIntoStack(ListNode head) {
ListNode tmp = head;
Deque<Integer> stack = new ArrayDeque<>();
while (tmp != null) {
stack.push(tmp.val);
tmp = tmp.next;
}
return stack;
}
}
public static class Solution2 {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
Stack<Integer> stack1 = popOntoStack(l1);
Stack<Integer> stack2 = popOntoStack(l2);
Stack<Integer> resultStack = add(stack1, stack2);
return buildResult(resultStack);
}
private ListNode buildResult(Stack<Integer> stack) {
ListNode prev = new ListNode(-1);
ListNode head = new ListNode(stack.pop());
prev.next = head;
while (!stack.isEmpty()) {
head.next = new ListNode(stack.pop());
head = head.next;
}
return prev.next;
}
private Stack<Integer> add(Stack<Integer> stack1, Stack<Integer> stack2) {
Stack<Integer> res = new Stack<>();
int carry = 0;
while (!stack1.isEmpty() || !stack2.isEmpty()) {
if (!stack1.isEmpty()) {
carry += stack1.pop();
}
if (!stack2.isEmpty()) {
carry += stack2.pop();
}
int value = carry;
if (carry > 9) {
value = carry % 10;
carry = 1;
} else {
carry = 0;
}
res.push(value);
}
if (carry != 0) {
res.add(carry);
}
return res;
}
private Stack<Integer> popOntoStack(ListNode head) {
ListNode temp = head;
Stack<Integer> stack = new Stack<>();
while (temp != null) {
stack.push(temp.val);
temp = temp.next;
}
return stack;
}
}
}