做者: 負雪明燭
id: fuxuemingzhu
我的博客: http://fuxuemingzhu.cn/node
題目地址:https://leetcode.com/problems/add-two-numbers-ii/description/python
You are given two non-empty linked lists representing two non-negative
integers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.git
You may assume the two numbers do not contain any leading zero, except the number 0 itself.app
Follow up:ide
What if you cannot modify the input lists? In other words, reversing the lists is not allowed.spa
Example: Input: (7 -> 2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 8 -> 0 -> 7
有兩個鏈表,分別是正序的十進制數字。如今要求兩個十位數字的和,要求返回的結果也是鏈表。code
選擇easy模式的話,能夠直接先求出和,再構建鏈表。orm
這麼作的前提是python中沒有最大的整數,因此不管怎麼着不會越界!ip
Python代碼以下:leetcode
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ num1 = '' num2 = '' while l1: num1 += str(l1.val) l1 = l1.next while l2: num2 += str(l2.val) l2 = l2.next add = str(int(num1) + int(num2)) head = ListNode(add[0]) answer = head for i in range(1, len(add)): node = ListNode(add[i]) head.next = node head = head.next return answer
能夠採用翻轉後變成 Add Two Numbers 的題目,可是題目說了最好別那麼作。那麼能夠使用一個棧來完成相似的操做。上一個題目的結果也是數字的倒序的,而這個題要求結果是正序,那麼加的過程當中採用頭插法,一直向頭部插入新的節點就行了。
步驟很是相似Add Two Numbers。
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ stack1 = [] stack2 = [] while l1: stack1.append(l1.val) l1 = l1.next while l2: stack2.append(l2.val) l2 = l2.next answer = None carry = 0 while stack1 and stack2: add = stack1.pop() + stack2.pop() + carry carry = 1 if add >= 10 else 0 temp = answer answer = ListNode(add % 10) answer.next = temp l = stack1 if stack1 else stack2 while l: add = l.pop() + carry carry = 1 if add >= 10 else 0 temp = answer answer = ListNode(add % 10) answer.next = temp if carry: temp = answer answer = ListNode(1) answer.next = temp return answer
二刷的時候使用的C++的vector來存儲的每一個節點的值,而後再作的加法。每次構建新節點使用的頭插法,因此結果一樣是題目要求的倒序。
C++代碼以下:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { vector<int> v1, v2; while (l1) { v1.push_back(l1->val); l1 = l1->next; } while (l2) { v2.push_back(l2->val); l2 = l2->next; } const int M = v1.size(), N = v2.size(); int i1 = M - 1, i2 = N - 1; int carry = 0; ListNode* dummy = new ListNode(0); ListNode* cur = dummy; while (i1 >= 0 || i2 >= 0 || carry) { int add = (i1 >= 0 ? v1[i1] : 0) + (i2 >= 0 ? v2[i2] : 0) + carry; carry = add >= 10 ? 1 : 0; add %= 10; ListNode* cur = new ListNode(add); cur->next = dummy->next; dummy->next = cur; if (i1 >= 0) --i1; if (i2 >= 0) --i2; } return dummy->next; } };
2. Add Two Numbers
989. Add to Array-Form of Integer
2018 年 2 月 26 日 2019 年 2 月 22 日 —— 這周結束了