Leetcode OJ #2 Add-Numbers

Source: Internet
Author: User

https://leetcode.com/problems/add-two-numbers/

Test instructions: Two linked lists represent two numbers,

2 4 3

5 6 4

Follow the rules of addition and carry them from left to right.

Output: and the linked list.


is still a water problem. Some special situations need to be considered (I think about it when I do the problem)

1, if two lengths, such as L1>L2, then need to L1 more than L2 out of the part plus carry copy into the ANS

2, 5+5, although the length is equal, but the most high


Unfortunately, WA had two rounds, because

The right one should be

int v = l1->val + L2->val +add;ans, val = v%10;
I write

int v = l1->val + L2->val;ans, val = v%10+add;

Real Two pen ... In addition, the data structure is really not ripe, you have to write an analysis of STL source code ...

Here is the test program

#include <cstdio> #include <cstring> #include <vector> #include <algorithm> #include < Iostream>using namespace Std;      struct ListNode {int val;      ListNode *next; ListNode (int x): Val (x), Next (NULL) {}}; ListNode * a=null, * b=null;class Solution {public:listnode* addtwonumbers (listnode* L1, listnode* L2) {int ad        d = 0;        listnode* ans = 0;        listnode* ret=0;        int f = 0; if (l1| |        L2) ans = new ListNode (0);            while (L1 && L2) {int v = l1->val + l2->val +add;            cout << "DEBUG" << l1->val << "<< l2->val << Endl;                 if (f = = 0) {ret = ans;                 f++;            ans, val = v%10;                }else{ans-next = new ListNode (0);                Ans = ans->next;                ans, val = v%10;            Ans->next = 0;            } add = V/10; L1 =l1->next;        L2 = l2->next;            }//finally need to process add if (L1 | | L2) {listnode* remain = L1? l1:l2;                while (remain) {//cout << "DEBUG" << remain->val << Endl;                int v = remain->val+add;//+ l2->val;                Ans->next = new ListNode (V%10);                add = V/10;                Ans = ans->next;                Ans->next = NULL;            remain = remain->next;                }} if (add) {ans->next = new ListNode (add);            Ans->next->next = 0;    } return ret;    }};int Main () {//freopen ("In.txt", "R", stdin);    String str1, str2;    CIN >> str1 >> str2;    ListNode *sta=null, *stb=null;    if (Str1.size ()) sta = a = new ListNode (0);        for (int i=0;i<str1.size (); i++) {a->val = str1[i]-' 0 '; A->next = (I==str1.size ()-1)?        Null:new ListNode (0);  A = a->next;  } if (Str2.size ()) STB = b = new ListNode (0);        for (int i=0;i<str2.size (); i++) {b->val = str2[i]-' 0 '; B->next = (i = = Str2.size ()-1)?        Null:new ListNode (0);    b = b->next;    } listnode* ans = ((new solution ())->addtwonumbers (STA,STB));//listnode* ans = NULL;        while (ans) {cout << ans->val << Endl;    ans = ans next; } return 0;}


Leetcode OJ #2 Add-Numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.