leetcode-algorithms-2 Add Two Numbers
阿新 • • 發佈:2018-11-14
leetcode-algorithms-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.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
解法
同時遍歷兩個連結串列,對值進行相加.得到的值%10就是新的連結串列的值,同時存下/10(只可能是1)的值,用於下個位數的增值.
/** * 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) { ListNode *l = nullptr; ListNode *lt = nullptr; ListNode *node1 = l1; ListNode *node2 = l2; int lastaddval = 0; while(true) { if (node1 == nullptr && node2 == nullptr) break; int nodeval1 = 0; int nodeval2 = 0; if (node1 != nullptr) { nodeval1 = node1->val; node1 = node1->next; } if (node2 != nullptr) { nodeval2 = node2->val; node2 = node2->next; } int sumval = nodeval1 + nodeval2 + lastaddval; lastaddval = sumval / 10; if (l == nullptr) { l = new ListNode(sumval % 10); lt = l; } else { ListNode *n = new ListNode(sumval % 10); lt->next = n; lt = n; } } if (lastaddval != 0) { ListNode *n = new ListNode(lastaddval); lt->next = n; lt = n; } return l; } };
時間複雜度: O(max(m,n)).m和n分別是l1和l2的長度.
空間複雜度: O(max(m,n)).