两数相加 II(链表版)
题目
两个非空链表来代表两个非负整数. 数字最高位位于链表开始位置. 它们的每个节点只存储一位数字, 将这两数相加会返回一个新的链表. 这两个数字都不会以零开头.
示例
输入: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4)
输出: 7 -> 8 -> 0 -> 7
题解
这道题是 两数相加(链表版) 的进阶版, 难点在于高位在前, 低位在后, 故无法直接使用加法规则. 为了使低位先出来, 自然会想到使用栈.
- 遍历两个链表, 将数据逐一 push 到栈中
- 对两个栈进行 pop, 得到的数字即为相同位置的数字
- 剩下的就是大数相加和栈的基本操作了
- JavaScript
- Rust
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function (l1, l2) {
const stack1 = []
const stack2 = []
while (l1 !== null) {
stack1.push(l1.val)
l1 = l1.next
}
while (l2 !== null) {
stack2.push(l2.val)
l2 = l2.next
}
const head = new ListNode(0)
let carry = 0
while (stack1.length !== 0 || stack2.length !== 0 || carry !== 0) {
const val1 = stack1.length === 0 ? 0 : stack1.pop()
const val2 = stack2.length === 0 ? 0 : stack2.pop()
const sum = val1 + val2 + carry
carry = (sum / 10) | 0
const node = new ListNode(sum % 10)
node.next = head.next
head.next = node
}
return head.next
}
// Definition for singly-linked list.
#[derive(PartialEq, Eq, Clone, Debug)]
pub struct ListNode {
pub val: i32,
pub next: Option<Box<ListNode>>,
}
impl ListNode {
#[inline]
fn new(val: i32) -> Self {
ListNode { next: None, val }
}
}
pub fn add_two_numbers(
l1: Option<Box<ListNode>>,
l2: Option<Box<ListNode>>,
) -> Option<Box<ListNode>> {
let mut a = Vec::new();
let mut b = Vec::new();
let mut ll = l1;
while let Some(x) = ll {
a.push(x.val);
ll = x.next;
}
let mut ll = l2;
while let Some(x) = ll {
b.push(x.val);
ll = x.next;
}
let mut carry = 0;
let mut head = ListNode::new(0);
while a.len() > 0 || b.len() > 0 || carry != 0 {
let mut a_num = 0;
let mut b_num = 0;
if a.len() > 0 {
a_num = a.pop().unwrap();
}
if b.len() > 0 {
b_num = b.pop().unwrap();
}
let sum = a_num + b_num + carry;
let mut node = ListNode::new(sum % 10);
node.next = head.next.clone();
head.next = Some(Box::new(node));
carry = sum / 10;
}
head.next
}