--- title: 【LeetCode】0002. Add Two Numbers date: 2018-12-20 is_modified: false disqus: cynthiahackmd categories: - "面試刷題" tags: - "LeetCode" --- {%hackmd @CynthiaChuang/Github-Page-Theme %} <br> 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. <!--more--> **Example:** ```python Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807. ``` <br> **Related Topics:** `Linked List` ## 解題邏輯與實作 這題不算難,就是兩個數字做相加,進位的話向後放,需要注意的就是兩個數字長度不相同以及最高位進位的情況。 比較討厭的大概就是很久沒有寫鏈結串列了,操作起來有點討厭。奇怪,為啥以前我會很喜歡寫鏈結串列呢 (*´・д・)? <br> 實做步驟如下: 1. 新增一個 fake 的 head 節點,並將 end 也指向這個節點。 2. 依序取出 input 的鏈結串列的節點與前一個位數的進位進行相加。 1. 若相加結果有進位,則記錄進位數。 2. 否則,將進位數計為 0。 3. 將計算結果個位數使用一個新的節點,添加到新的鏈結串列後,並將 end 指到這個節點。 4. 返回第二步,繼續計算,若鏈結串列取出的節點不存在,則以 0 進行計算,直到兩陣列都計算完畢。 5. 計算結束後回傳 head 的下一個節點。 <br> ```python= class Solution: def addTwoNumbers(self, l1, l2): carry = 0 result = end = ListNode(0) while l1 or l2 or carry: v1 = l1.val if l1 else 0 v2 = l2.val if l2 else 0 carry, val = divmod(v1 + v2 + carry, 10) l1 = l1.next if l1 else l1 l2 = l2.next if l2 else l2 end.next = ListNode(val) end = end.next return result.next ``` ## 其他連結 1. [【LeetCode】0000. 解題目錄](/x62skqpKStKMxRepJ6iqQQ) <br><br> > **本文作者**: 辛西亞.Cynthia > **本文連結**: [辛西亞的技能樹](https://cynthiachuang.github.io/LeetCode-0002-Add-Two-Numbers) / [hackmd 版本](https://hackmd.io/@CynthiaChuang/LeetCode-0002-Add-Two-Numbers) > **版權聲明**: 部落格中所有文章,均採用 [姓名標示-非商業性-相同方式分享 4.0 國際](https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en) (CC BY-NC-SA 4.0) 許可協議。轉載請標明作者、連結與出處!
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up