445. 两数相加 II

题目描述

原题
给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。
你可以假设除了数字 0 之外,这两个数字都不会以零开头。
进阶:
如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转。
示例:
1
输入:(7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
2
输出:7 -> 8 -> 0 -> 7
Copied!

题解

方法一

1
/**
2
* Definition for singly-linked list.
3
* public class ListNode {
4
* int val;
5
* ListNode next;
6
* ListNode() {}
7
* ListNode(int val) { this.val = val; }
8
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9
* }
10
*/
11
class Solution {
12
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
13
//先翻转链表 然后两个链表相加
14
//再翻转结果链表
15
l1 = reverse(l1);
16
l2 = reverse(l2);
17
ListNode dummyNode = new ListNode();
18
ListNode curr = dummyNode;
19
int carry = 0;
20
while(l1 != null || l2 !=null){
21
int x = l1!=null ? l1.val:0;
22
int y = l2!=null ? l2.val:0;
23
int sum = x + y + carry;
24
carry = sum / 10;
25
curr.next = new ListNode(sum%10);
26
curr = curr.next;
27
if(l1!=null) l1 = l1.next;
28
if(l2!=null) l2 = l2.next;
29
}
30
if(carry>0){
31
curr.next = new ListNode(carry);
32
}
33
return reverse(dummyNode.next);
34
}
35
36
private ListNode reverse(ListNode node){
37
ListNode pre = null;
38
ListNode curr = node;
39
while(curr!=null){
40
ListNode next = curr.next;
41
curr.next = pre;
42
pre = curr;
43
curr = next;
44
}
45
return pre;
46
}
47
}
Copied!

方法二

1
/**
2
* Definition for singly-linked list.
3
* public class ListNode {
4
* int val;
5
* ListNode next;
6
* ListNode() {}
7
* ListNode(int val) { this.val = val; }
8
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9
* }
10
*/
11
class Solution {
12
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
13
LinkedList<ListNode> stack1 = addToStack(l1);
14
LinkedList<ListNode> stack2 = addToStack(l2);
15
LinkedList<ListNode> stack3 = new LinkedList<>();
16
int carry = 0;
17
while(!stack1.isEmpty() || !stack2.isEmpty()||carry > 0){
18
int x = !stack1.isEmpty() ? stack1.pop().val:0;
19
int y = !stack2.isEmpty() ? stack2.pop().val:0;
20
int sum = x + y + carry;
21
carry = sum / 10;
22
stack3.push(new ListNode(sum % 10));
23
}
24
ListNode dummyNode = new ListNode();
25
ListNode curr = dummyNode;
26
while(!stack3.isEmpty()){
27
curr.next = stack3.pop();
28
curr = curr.next;
29
}
30
return dummyNode.next;
31
}
32
private LinkedList<ListNode> addToStack(ListNode node){
33
LinkedList<ListNode> stack = new LinkedList<>();
34
while(node!=null){
35
stack.push(node);
36
node = node.next;
37
}
38
return stack;
39
}
40
}
Copied!
1
class Solution {
2
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
3
//栈里面可以直接存储值 不用存储ListNode
4
Deque<Integer> stack1 = new LinkedList<Integer>();
5
Deque<Integer> stack2 = new LinkedList<Integer>();
6
while (l1 != null) {
7
stack1.push(l1.val);
8
l1 = l1.next;
9
}
10
while (l2 != null) {
11
stack2.push(l2.val);
12
l2 = l2.next;
13
}
14
int carry = 0;
15
ListNode pre = null;
16
while (!stack1.isEmpty() || !stack2.isEmpty() || carry != 0) {
17
int a = stack1.isEmpty() ? 0 : stack1.pop();
18
int b = stack2.isEmpty() ? 0 : stack2.pop();
19
int sum = a + b + carry;
20
carry = sum / 10;
21
sum %= 10;
22
//这里借助了链表翻转的思路
23
ListNode curr = new ListNode(sum);
24
curr.next = pre;
25
pre = curr;
26
}
27
return pre;
28
}
29
}
Copied!
最近更新 4mo ago