2.两数相加

4/11/2023 力扣

# 1 两数之和

给你两个 非空 的链表,表示两个非负的整数。它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字。

请你将两个数相加,并以相同形式返回一个表示和的链表。

你可以假设除了数字 0 之外,这两个数都不会以 0 开头。

示例 1:

输入:l1 = [2,4,3], l2 = [5,6,4]
输出:[7,0,8]
解释:342 + 465 = 807.
1
2
3

示例 2:

输入:l1 = [0], l2 = [0]
输出:[0]
1
2

示例 3:

输入:l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
输出:[8,9,9,9,0,0,0,1]
1
2

解题思路:创建两个节点head、temp,遍历链表判断是否为空,将数值相加后num%10存在新的链表中,再将数值num/10,大于十的部分保留下次循环加上


java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(-1),temp = head;
        int num = 0;

        while(l1 != null || l2 != null || num!=0){
            if(l1 != null){
                num+=l1.val;
                l1 = l1.next;
            }
            if(l2 !=null){
                num+=l2.val;
                l2 = l2.next;
            }
            temp.next = new ListNode(num%10);
            temp = temp.next;
            num/=10;
        }
        return head.next;
    }
}
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

js

/**
 * 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 head = new ListNode(-1);
    let temp = head;
    let num = 0;

    while(l1 != null||l2 !=null || num !=0){
        if(l1 != null){
            num+=l1.val;
            l1 = l1.next;
        }
        if(l2 != null){
           num += l2.val;
           l2 = l2.next; 
        }
        temp.next = new ListNode(num%10);
        temp = temp.next;
        num = parseInt(num/10);
    }

    return head.next;
};
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

Ts

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     val: number
 *     next: ListNode | null
 *     constructor(val?: number, next?: ListNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.next = (next===undefined ? null : next)
 *     }
 * }
 */

function addTwoNumbers(l1: ListNode | null, l2: ListNode | null): ListNode | null {
    let head:ListNode = new ListNode(-1);
    let temp:ListNode = head;
    let num:number = 0;

    while(l1 != null || l2 !=null || num != 0){
        if(l1 != null){
            num += l1.val;
            l1 = l1.next;
        }
        if(l2 != null){
            num+=l2.val;
            l2 = l2.next;
        }
        temp.next = new ListNode(num%10);
        temp = temp.next;
        num = num/10 | 0
        // num = Math.floor(num/10);
    }
    return head.next;
};

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
Last Updated: 4/13/2023, 10:41:52 AM