[沒事練練][Leetcode-2] Add Two Numbers

 

題目要求

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

  • 啞節點

  • 鏈表

獲得較好閱讀體驗,可以通過左下角的閱讀原文按鈕。如果覺得文章還有收穫的話,可以留言以及友情點擊留言區小廣告,點下即可哈~如果你覺得我推薦的商品有需求的話,也可以走文中鏈接購買哦。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章