题目要求
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 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.
给出两个非空的链表用来表示两个非负的整数。其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。您可以假设除了数字 0 之外,这两个数都不会以 0开头。
链接:https://leetcode-cn.com/problems/add-two-numbers/
示例
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807.
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807
题解思路
题目就是将一个整数相加的问题,使用链表来呈现了。
我想到的方法就是去遍历两个链表,每两个数字相加后,会产生一个进位 carry,进位会带到下一轮继续使用,同时计算后的结果会成为新链表的节点,例如 4 和 6 相加,carry 1带入下一轮继续使用, 0 成为新链表的其中一个节点。
整体难度不是很大,注意考虑两个链表不是等长的,链表循环到末尾也可能产生进位,出现新节点,以及使用额外节点保存头结点即可解决这个问题。
示例代码:
Java版本
1/**
2 * Definition for singly-linked list.
3 * public class ListNode {
4 * int val;
5 * ListNode next;
6 * ListNode(int x) { val = x; }
7 * }
8 */
9class Solution {
10 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
11 ListNode current = new ListNode(0);
12 ListNode p = l1, q = l2;
13 int carry = 0;
14 ListNode dumpHead = current; // 额外的哑节点用来保存头部节点,因为最后要进行返回
15 while (p != null || q != null) {
16 int number1 = (p != null) ? p.val : 0;
17 int number2 = (q != null) ? q.val : 0;
18 int result = number1 + number2 + carry;
19 carry = result / 10;
20 ListNode next = new ListNode(result % 10);
21 current.next = next;
22 current = current.next; // 继续往下移动
23
24 // 原有两个列表的节点继续往下移动
25 if (p != null) {
26 p = p.next;
27 }
28 if (q != null) {
29 q = q.next;
30 }
31 }
32
33 // 如果最后链表的数字加完还有进位的话,那么还需要额外再创建出一个节点
34 if (carry > 0) {
35 current.next = new ListNode(carry);
36 }
37 return dumpHead.next;
38 }
39}
Python版本
1# Definition for singly-linked list.
2# class ListNode:
3# def __init__(self, x):
4# self.val = x
5# self.next = None
6
7class Solution:
8 def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
9 current = ListNode(0)
10 dumpHead = current
11 p = l1
12 q = l2
13 carry = 0
14 while p or q :
15 number1 = (p.val if p else 0)
16 number2 = (q.val if q else 0)
17 result = number1 + number2 + carry
18 carry = result // 10
19 current.next = ListNode(result % 10)
20 current = current.next
21
22 if p is not None:
23 p = p.next
24 if q is not None:
25 q = q.next
26
27 if carry:
28 current.next = ListNode(carry)
29
30 return dumpHead.next
复杂度分析:
时间复杂度:O(max(m,n)),m和n分别为两个链表的长度,最多循环max(m,n)次。
空间复杂度:O(max(m,n)),因为引入额外的链表存储,最多存储空间为max(m,n)+1,因为最后一位可能额外产生新节点。
Tag
-
哑节点
-
链表
获得较好阅读体验,可以通过左下角的阅读原文按钮。如果觉得文章还有收获的话,可以留言以及友情点击留言区小广告,点下即可哈~如果你觉得我推荐的商品有需求的话,也可以走文中链接购买哦。