Leetcode2 ADD Numbers

Source: Internet
Author: User

The general meaning of the question is, enter two list, these two list is two reverse order number, for example, 1->2->4 is 421. Then the two linked list is flipped and added, stored in the listing, also in reverse order into the list, return it, just beginning test instructions understand wrong, WA two times, The topic gives a set of data is confusing, that is, 243+564 and 432+465 results are 807, so just at the beginning I thought the input two linked list of the number of positive sequence, just flip the result can be. In fact, this problem and large integer sum difference is not too much, as long as the consideration of the carry is no problem.

First edition the code below, more cumbersome, there are some test statements:

#include <stdio.h> #include <stdlib.h> #include <string.h> struct ListNode {int val;      ListNode *next;        ListNode (int x): Val (x), Next (NULL) {}};class solution {public:listnode* addtwonumbers (listnode* L1, listnode* L2) { int num1[1000],num2[1000],sum[1000],temp[1000];//store L1,L2,L1+L2 Separately, flip the list with int i,j;//loop variable int len1,len2        , the len;//stores Len (L1), Len (L2), Max (len1,len2) int jinwei = 1;//carry's flag bit memset (num1,-1,sizeof (NUM1));        memset (num2,-1,sizeof (Num2));        memset (sum,-1,sizeof (Sum));                memset (temp,-1,sizeof (temp));            for (i = 1;; i++)//convert L1 to an array Num1 {temp[i] = l1->val;            if (L1->next = = NULL) {break;        } L1 = l1->next;        } len1 = i;           for (i = 1; i<=len1; i++) {num1[i] = temp[len1-i+1];        printf ("num1:%d\n", Num1[i]); } memset (Temp,-1,sizeof (temp));            for (i = 1;; i++)//convert L2 to an array Num2 {temp[i] = l2->val;            if (L2->next = = NULL) {break;        } L2 = l2->next;        } len2 = i;           for (i = 1; i<=len2; i++) {num2[i] = temp[len2-i+1];        printf ("num2:%d\n", Num2[i]);            } if (LEN2&LT;=LEN1)//based on length, short length to the top of length, the NUM1 and Num2 bitwise add {len = len1;            j = len1;            for (i = 1; i<=len1; i++) {sum[i] = Num1[i];            } for (i = len2; i>0; i--, j--) {sum[j] = Sum[j] + num2[i];            }} else {len = len2;            j = len2;            for (i = 1; i<=len2; i++) {sum[i] = Num2[i];            } for (i = len1; i>0; i--, j--) {sum[j] = Sum[j] + num1[i]; }}/*for (i = 0; i<=lEn        i++) {printf ("sum:%d\n", Sum[i]);                }*/for (i=len;i>0;i--)//Handle Sum's carry problem {if (sum[i-1] = = -1&& (SUM[I]/10)) {                sum[i-1]++;            Jinwei = 0;            } Sum[i-1] + = (SUM[I]/10);        Sum[i]%= 10;        }/*for (i = 0; i<=len; i++) {printf ("sum:%d\n", Sum[i]);        }*/ListNode Head (0);        ListNode *pre = &head;            for (i=len;i>=jinwei;i--)//convert Sum array to linked list {Pre->next = new ListNode (Sum[i]);        Pre = pre->next;            }/* while (1) {printf ("result:%d\n", head.next->val);        if (Head.next->next = = NULL) {break;        } Head.next = head.next->next;            }*/return head.next; }};int Main () {/*listnode list1 (1); listnode* p = &list1;p->next = new ListNode (8);p = P->next;p->next = new LISTNOde (4);p = P->next;p->next = new ListNode (7);p = p->next; *//*listnode* test = &list1;while (1) {printf ("%d", test->val), if (Test->next = = NULL) {break;} Test = Test->next;}    */ListNode List1 (5);    listnode* p = &list1; P->next = new ListNode (8); ListNode List2 (5); Solution TT; ListNode *result = tt.addtwonumbers (&AMP;LIST1,&AMP;LIST2);/*while (1) {printf ("%d", result->val); if (result-> Next = = NULL) {break;} result = Result->next;} */return 0;}

The final version, after simplification:

Class Solution {public:    void append (listnode* L3, int num) {        ListNode *end = new ListNode (num);        L3->next = end;    }    listnode* addtwonumbers (listnode* L1, listnode* L2) {        int carry = 0;        ListNode *l3 = new ListNode (0);        ListNode *pl3 = L3;        int A, B, c;//while        (L1 | | l2 | | carry) {            if (L1) a = l1->val;            else    a = 0;            if (L2) b = l2->val;            else    b = 0;            c = (A + b + carry)% 10;//direct reverse order calculation sequence, deposited after the remainder iude            append (Pl3, c);//Put C in the list            pl3 = pl3->next;            Carry = (A + B + carry)/10;//will carry the rounding in carry            if (l1) L1 = l1->next;            if (L2) L2 = l2->next;        }        ListNode *ret = l3->next;        Delete L3;        return ret;    }};


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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