2. 两数相加
给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。
请你将两个数相加,并以相同形式返回一个表示和的链表。
你可以假设除了数字 0 之外,这两个数都不会以 0 开头。
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
示例 1:输入:l1 = [2,4,3], l2 = [5,6,4]输出:[7,0,8]解释:342 + 465 = 807.
示例 2:输入:l1 = [0], l2 = [0]输出:[0]
示例 3:输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]输出:[8,9,9,9,0,0,0,1]提示:
每个链表中的节点数在范围 [1, 100] 内
0 <= Node.val <= 9
题目数据保证列表表示的数字不含前导零
Constraints:
The number of nodes in each linked list is in the range [1, 100].
0 <= Node.val <= 9
It is guaranteed that the list represents a number that does not have leading zeros.
解题方法
方法一: 双指针
/** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } *//** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */var addTwoNumbers = function (l1, l2) { let pre = new ListNode(0); let cur = pre; let carry = 0;
while (l1 || l2) { const x = l1 ? l1.val : 0; const y = l2 ? l2.val : 0; const sum = x + y + carry; cur.next = new ListNode(sum % 10); carry = Math.floor(sum / 10); cur = cur.next;
if (l1) { l1 = l1.next; }
if (l2) { l2 = l2.next; } }
if (carry) { cur.next = new ListNode(carry); }
return pre.next;};方法二:三指针
/** * Definition for singly-linked list. * function ListNode(val, next) { * this.val = (val===undefined ? 0 : val) * this.next = (next===undefined ? null : next) * } *//** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */var addTwoNumbers = function (l1, l2) { let l3 = new ListNode(0);
let p1 = l1; let p2 = l2; let p3 = l3;
let carry = 0;
while (p1 || p2) { let x = p1 ? p1.val : 0; let y = p2 ? p2.val : 0;
let sum = x + y + carry; p3.next = new ListNode(sum % 10); carry = Math.floor(sum / 10);
if (p1) { p1 = p1.next; } if (p2) { p2 = p2.next; } p3 = p3.next; }
if (carry) { p3.next = new ListNode(carry); } return l3.next;};