-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddList.java
More file actions
46 lines (41 loc) · 1.13 KB
/
AddList.java
File metadata and controls
46 lines (41 loc) · 1.13 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
package linkedlist;
import linkedlist.common.Node;
import java.util.Stack;
/**
* Created by LXF on 2017/7/7.
*/
public class AddList {
public Node AddList1(Node node1, Node node2) {
Stack<Integer> nodeStack1 = new Stack<Integer>();
Stack<Integer> nodeStack2 = new Stack<Integer>();
while (node1 != null) {
nodeStack1.push(node1.value);
node1 = node1.next;
}
while (node2 != null) {
nodeStack2.push(node2.value);
node2 = node2.next;
}
int ca = 0;
int n1= 0;
int n2= 0;
int n= 0;
Node node = null;
Node pre = null;
while (!nodeStack1.isEmpty() || !nodeStack2.isEmpty()) {
n1 = nodeStack1.isEmpty() ? 0 : nodeStack1.pop();
n2 = nodeStack2.isEmpty() ? 0 : nodeStack2.pop();
n = n1 + n2 + ca;
pre = node;
node = new Node(n % 10);
node.next = node;
ca = n/10;
}
if (ca == 1) {
pre = node;
node = new Node(1);
node.next = pre;
}
return node;
}
}