-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddTwoNumbers.java
More file actions
139 lines (115 loc) · 3.06 KB
/
AddTwoNumbers.java
File metadata and controls
139 lines (115 loc) · 3.06 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
2. Add Two Numbers QuestionEditorial Solution My Submissions
Total Accepted: 178094
Total Submissions: 716094
Difficulty: Medium
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Hide Company Tags Amazon Microsoft Bloomberg Airbnb Adobe
Hide Tags Linked List Math
Hide Similar Problems (M) Multiply Strings (E) Add Binary (E) Sum of Two Integers
*/
import datastructure.ListNode;
import java.util.Stack;
/**
* Definition for singly-linked list. public class datastructure.ListNode { int
* val; datastructure.ListNode next; datastructure.ListNode(int x) { val = x; }
* }
*/
// LeetCode, Add Two Numbers
// 跟Add Binary 很类似
// 时间复杂度O(m+n),空间复杂度O(1)
public class AddTwoNumbers {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode cur = dummy;
int carry = 0;
while (l1 != null || l2 != null || carry != 0) {
int sum = carry;
if (l1 != null) {
sum += l1.val;
l1 = l1.next;
}
if (l2 != null) {
sum += l2.val;
l2 = l2.next;
}
carry = sum / 10;
sum = sum % 10;
ListNode node = new ListNode(sum);
cur.next = node;
cur = cur.next;
}
return dummy.next;
}
/**
* Bloomberg phone interview 两个LinkedList 代表两个integer
* 然后在不reverse,不转成String或int的情况下做相加操作 返回新的LinkedList 前面发面经的同学已经交代了用stack
*
* @param l1
* ,l2
*/
public ListNode addTwoNumbers_bb(ListNode l1, ListNode l2) {
ListNode res = new ListNode(0);
Stack<Integer> stk1 = new Stack<>();
Stack<Integer> stk2 = new Stack<>();
Stack<ListNode> stk3 = new Stack<>();
while (l1 != null || l2 != null) {
if (l1 != null) {
stk1.push(l1.val);
l1 = l1.next;
}
if (l2 != null) {
stk2.push(l2.val);
l2 = l2.next;
}
}
int carry = 0;
while (!stk1.empty() || !stk2.empty() || carry != 0) {
int sum = carry;
if (!stk1.empty()) {
sum += stk1.pop();
}
if (!stk2.empty()) {
sum += stk2.pop();
}
carry = sum / 10;
sum = sum % 10;
ListNode node = new ListNode(sum);
stk3.push(node);
}
ListNode cur = res;
while (!stk3.empty()) {
cur.next = stk3.pop();
cur = cur.next;
}
return res.next;
}
public void print(ListNode node) {
while (node != null) {
if (node.next != null) {
System.out.print(node.val + "->");
node = node.next;
} else if (node.next == null) {
System.out.print(node.val);
node = node.next;
}
}
}
public static void main(String[] args) {
ListNode l1 = new ListNode(2);
ListNode n2 = new ListNode(4);
ListNode n3 = new ListNode(3);
l1.next = n2;
n2.next = n3;
ListNode l2 = new ListNode(5);
ListNode n4 = new ListNode(6);
ListNode n5 = new ListNode(4);
l2.next = n4;
n4.next = n5;
AddTwoNumbers slt = new AddTwoNumbers();
ListNode result = slt.addTwoNumbers_bb(l1, l2);
slt.print(result);
}
}