05 August 2008
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:
1
2
Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 8 -> 0 -> 7
这道题跟前面一道题比,significant digit在前面了,所以个位就在最后。用stack的办法来让最后的位数先相加即可。时间O(2m + 2n) = O(m + n);空间创建了两个新的stack,O(m+n)。
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
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
if (l1 == null || l2 == null) {
return l1 == null ? l2 : l1;
}
Stack<Integer> stack1 = new Stack<>();
Stack<Integer> stack2 = new Stack<>();
while (l1 != null) {
stack1.push(l1.val);
l1 = l1.next;
}
while (l2 != null) {
stack2.push(l2.val);
l2 = l2.next;
}
ListNode list = new ListNode(-1);
int carry = 0;
while (!stack1.isEmpty() || !stack2.isEmpty()) {
int sum = carry;
if (!stack1.isEmpty()) {
sum += stack1.pop();
}
if (!stack2.isEmpty()) {
sum += stack2.pop();
}
list.val = sum % 10;//当前结点的val
carry = sum / 10;
ListNode head = new ListNode(carry);//new一个head结点,注意这里的val值需要是carry,两个stack都加完循环跳出后,有进位的话需要返回有正确的val的第一个结点
head.next = list;//将上一步new出来的head结点插入到当前结点的前面
list = head;//将当前结点的更新为前面一个结点
}
if (carry != 0) {//两个stack中的数都加完了,如果还有进位的话就返回当前结点
return list;
}
return list.next;//没有进位的话,当前结点list的val为上一轮的carry==0,这时候返回list.next
}
}