WEEK#7 Linked List_Add Two Numbers

Description of the Problem

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 contains 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.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8


Thinking Process

From given input above, we should calculate result = 807 = 342 + 465, and construct a linked list by inserting the result's digits in reverse order.
In order to solve this problem, we first need to retrieve the entries in the two given linked lists and reverse the order to get two numbers to add. Then we add these two numbers up, getting the result, and construct a linked list from it.

When it comes to reversing, stack is the first thing that comes to my mind, for its characteristic of FILO.


Inefficient Solution

class Solution {
public:
    string BigIntAdding(string int1, string int2) {
        string adder = int1.length() >= int2.length() ? int1 : int2;
        string addee = int1.length() < int2.length() ? int1 : int2;
        int diff = adder.length() - addee.length();
        while (diff--)
            addee.insert(addee.begin(), '0');
        string overflow;
        overflow.resize(adder.length());
        string result;
        result.resize(adder.length());
        for (auto i : overflow)
            i = '0';
        for (int i = adder.size() - 1; i >= 1; i--) {
            int tempadder = atoi(adder.substr(i, 1).c_str());
            int tempaddee = atoi(addee.substr(i, 1).c_str());
            int tempresult = tempadder + tempaddee + atoi(overflow.substr(i,1).c_str());
            overflow[i - 1] = 48 + tempresult / 10;
            result[i] = 48 +tempresult%10;
        }
        int tempadder = atoi(adder.substr(0, 1).c_str());
        int tempaddee = atoi(addee.substr(0, 1).c_str());
        int tempresult = tempadder + tempaddee + atoi(overflow.substr(0, 1).c_str());
        result[0] = tempresult%10 + '0';
        if (tempresult >= 10)
            result.insert(result.begin(), 1 + '0');
        return result;
    }

    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        stack<int> sta1;
        stack<int> sta2;
        while (l1 != NULL) {
            sta1.push(l1->val);
            l1 = l1->next;
        }
        while (l2 != NULL) {
            sta2.push(l2->val);
            l2 = l2->next;
        }
        string int1, int2;
        while (!sta1.empty()) {
            int temp1;
            temp1 = sta1.top();
            sta1.pop();
            int1 += to_string(temp1);
        }
        while (!sta2.empty()) {
            int temp2;
            temp2 = sta2.top();
            sta2.pop();
            int2 += to_string(temp2);
        }
        long long int i1 = atoi(int1.c_str());
        long long int i2 = atoi(int2.c_str());
        long long int result = i1 + i2;
        string tempt = to_string(result);
        string res;
        if (tempt.length() >= 8)
            res = BigIntAdding(int1, int2);
        else
            res = tempt;
        string reverse;
        for (int i = 0; i < res.size(); i++) {
            reverse += res[res.size() - 1 - i];
        }
        int count = 0;
        ListNode* Head = new ListNode(atoi(reverse.substr(count++, 1).c_str()));
        ListNode* temp = Head;
        while (count < reverse.size()) {
            Head->next = new ListNode(atoi(reverse.substr(count++, 1).c_str()));
            Head = Head->next;
        }
        return temp;
    }
};
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,143評(píng)論 0 23
  • 金黃的花兒 在陽(yáng)光 雨露下滋養(yǎng) 那返青的約定 是否依然堅(jiān)守 睡夢(mèng)中 我看清了您的臉 卻是如此僵硬滄桑 是我們彼此...
    小草姐姐閱讀 402評(píng)論 0 0
  • 你說(shuō)的話 總是像下診斷書(shū)一樣 時(shí)而好轉(zhuǎn) 時(shí)而惡化 我有一只眼睛 看到你站在 天堂的門(mén)口 地獄的門(mén)口 檢查我是否有一...
    野馬王閱讀 485評(píng)論 0 0
  • 這世間不缺乏美,只是缺少發(fā)現(xiàn)美的眼睛。即使有了發(fā)現(xiàn)美的眼睛,在發(fā)現(xiàn)美之后,我們能做些什么呢?如果只是欣賞一下,過(guò)后...
    楊凱紅a閱讀 1,285評(píng)論 0 1
  • It's very cool at the morning.The weather is windy .I get...
    學(xué)霸不懂學(xué)渣的痛閱讀 185評(píng)論 0 1

友情鏈接更多精彩內(nèi)容