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)系作者
  • 序言:七十年代末壮莹,一起剝皮案震驚了整個(gè)濱河市驻右,隨后出現(xiàn)的幾起案子榜聂,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,265評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件脉让,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)刁赦,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,078評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)闻镶,“玉大人甚脉,你說(shuō)我怎么就攤上這事∶” “怎么了牺氨?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,852評(píng)論 0 347
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)墩剖。 經(jīng)常有香客問(wèn)我猴凹,道長(zhǎng),這世上最難降的妖魔是什么岭皂? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,408評(píng)論 1 283
  • 正文 為了忘掉前任郊霎,我火速辦了婚禮,結(jié)果婚禮上爷绘,老公的妹妹穿的比我還像新娘歹篓。我一直安慰自己,他們只是感情好揉阎,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,445評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布庄撮。 她就那樣靜靜地躺著,像睡著了一般毙籽。 火紅的嫁衣襯著肌膚如雪洞斯。 梳的紋絲不亂的頭發(fā)上吗铐,一...
    開(kāi)封第一講書(shū)人閱讀 49,772評(píng)論 1 290
  • 那天罢绽,我揣著相機(jī)與錄音跑芳,去河邊找鬼放前。 笑死,一個(gè)胖子當(dāng)著我的面吹牛显拳,可吹牛的內(nèi)容都是我干的失息。 我是一名探鬼主播树枫,決...
    沈念sama閱讀 38,921評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼螟加,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼徘溢!你這毒婦竟也來(lái)了吞琐?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,688評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤然爆,失蹤者是張志新(化名)和其女友劉穎站粟,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體曾雕,經(jīng)...
    沈念sama閱讀 44,130評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡奴烙,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,467評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了剖张。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片切诀。...
    茶點(diǎn)故事閱讀 38,617評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖搔弄,靈堂內(nèi)的尸體忽然破棺而出幅虑,到底是詐尸還是另有隱情,我是刑警寧澤肯污,帶...
    沈念sama閱讀 34,276評(píng)論 4 329
  • 正文 年R本政府宣布翘单,位于F島的核電站吨枉,受9級(jí)特大地震影響蹦渣,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜貌亭,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,882評(píng)論 3 312
  • 文/蒙蒙 一柬唯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧圃庭,春花似錦锄奢、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,740評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至书在,卻和暖如春灰伟,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背儒旬。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,967評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工栏账, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人栈源。 一個(gè)月前我還...
    沈念sama閱讀 46,315評(píng)論 2 360
  • 正文 我出身青樓挡爵,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親甚垦。 傳聞我的和親對(duì)象是個(gè)殘疾皇子茶鹃,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,486評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容

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