forked from terrytong0876/LintCode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd Two Numbers II.java
More file actions
105 lines (89 loc) · 2.48 KB
/
Add Two Numbers II.java
File metadata and controls
105 lines (89 loc) · 2.48 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
M
LinkedList并没有反过来,那么自己反:
方向相反。巧用stack.
做加法都一样:
1. carrier
2. carrier = (rst + carrier) / 10
3. rst = (rst + carrier) % 10
```
/*
You have two numbers represented by a linked list, where each node contains a single digit.
The digits are stored in forward order, such that the 1's digit is at the head of the list.
Write a function that adds the two numbers and returns the sum as a linked list.
Example
Given 6->1->7 + 2->9->5. That is, 617 + 295.
Return 9->1->2. That is, 912.
Tags Expand
Linked List High Precision
*/
/*
Thoughts: Different from Add Two Numbers I, which is in reverse order.
6 1 7
2 9 5
8 10 12
put the 2 linked list in 2 stacks. process the reversed list.
Save into another result stack.
At the end, return the actual order.
O(n)
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
/**
* @param l1: the first list
* @param l2: the second list
* @return: the sum list of l1 and l2
*/
public ListNode addLists2(ListNode l1, ListNode l2) {
if (l1 == null && l2 == null) {
return null;
} else if (l1 == null || l2 == null) {
return l1 == null ? l2 : l1;
}
Stack<ListNode> result = new Stack<ListNode>();
Stack<ListNode> s1 = new Stack<ListNode>();
Stack<ListNode> s2 = new Stack<ListNode>();
while (l1 != null) {
s1.push(l1);
l1 = l1.next;
}
while (l2 != null) {
s2.push(l2);
l2 = l2.next;
}
int carrier = 0;
while(!s1.isEmpty() || !s2.isEmpty()){
int sum = 0;
if (!s1.isEmpty() && !s2.isEmpty()) {
sum += s1.pop().val + s2.pop().val;
} else if (!s1.isEmpty()) {
sum += s1.pop().val;
} else {
sum += s2.pop().val;
}
result.push(new ListNode((sum + carrier) % 10));//2, 1, 9
carrier = (sum + carrier) / 10; // 12/10 = 1; 11/10 = 1; (8+1)/ 10 = 0
}
if (carrier == 1) {
result.push(new ListNode(carrier));
}
//return results:
ListNode node = new ListNode(0);
ListNode dummy = node;
while (!result.isEmpty()) {//219
node.next = result.pop();
node = node.next;
}
return dummy.next;
}
}
```