2. Two numbers added

Source: Internet
Author: User

2. Two numbers added

A two non-empty list is given to represent two non-negative integers. The digits are stored in reverse order, with each node storing only a single number. Adds two numbers to return a new linked list.

you can assume that except for the number 0, none of the two numbers will start with 0.

Example:

Input: (2, 4, 3) + (5, 6, 4)

output: 7, 0,8

Cause:342 + 465 = 807

Solution Solutions

Method: Elementary Mathematics

Ideas

We use variables to keep track of carry and to simulate the process of adding bits from a header that contains the least significant bit.

Figure 1, visualization of the two-number addition method: 342 + 465 = 807342+465=807, each node contains a number, and the digits are stored in reverse order.

Algorithm

just as you calculate two numbers on a paper and that, we start by adding the least significant bit, which is the table header of the list l1l1 and l2l2. Since each digit should be at 0 \ldots 90 ... In the range of 9, we calculate the number of two and the "overflow" may occur. For example, 5 + 7 = 125+7=12. In this case, we will set the value of the current bit to 22 and carry carry = 1carry=1 into the next iteration. The carry Carrycarry must be 00 or 11, because two numbers are added (taking into account the rounding) that can occur for the largest and 9 + 9 + 1 = 199+9+1=19.

Please pay special attention to the following situations:

Test Cases

Description

l1=[0,1]
l2=[0,1,2]

When a list is longer than another list.

L1=[]
l2=[0,1]

When a list is empty, an empty list appears.

L1=[9,9]
L2=[1]

The sum operation may end up with additional rounding, which can easily be forgotten

1 classSolution {2  Public:3listnode* addtwonumbers (listnode* L1, listnode*L2) {4ListNode *p1 = L1, *p2 =L2;5         intsum, CF, remain;6 7         //Digit8sum = P1->val + p2->Val;9CF = SUM/Ten;Tenremain = sum%Ten; One  AListNode *ret =NewListNode (remain); -ListNode *P3 =ret; -  theP1 = p1->Next; -P2 = p2->Next; -  -          while(P1! = NULL && P2! =NULL) +         { -sum = p1->val + P2->val +CF; +CF = SUM/Ten; Aremain = sum%Ten; at              -P3->next =NewListNode (remain); -P3 = p3->Next; -  -P1 = p1->Next; -P2 = p2->Next; in         } -  to          while(P1! =NULL) +         { -             if(cf! =0) the             { *sum = P1->val +CF; $CF = SUM/Ten;Panax Notoginsengremain = sum%Ten; -  theP3->next =NewListNode (remain); +P3 = p3->Next; AP1 = p1->Next; the             } +             Else -             { $P3->next =NewListNode (p1->val); $P3 = p3->Next; -P1 = p1->Next; -             } the         } -          while(P2! =NULL)Wuyi         { the             if(cf! =0) -             { Wusum = P2->val +CF; -CF = SUM/Ten; Aboutremain = sum%Ten; $  -P3->next =NewListNode (remain); -P3 = p3->Next; -P2 = p2->Next; A             } +             Else the             { -P3->next =NewListNode (p2->val); $P3 = p3->Next; theP2 = p2->Next; the             } the         } the  -         if(cf!=0) inP3->next =NewListNode (1); the  the         returnret; About     } the};


Analysis of Complexity:

L time complexity:O (\max (m, n)) O (max (m,n)), assuming mm and nn represent the lengths of L1l1 and L2L2 respectively, the algorithm above repeats at most \max (M, N) Max (m,n) times.

L Spatial complexity:O (\max (m, n)) O (max (m,n)), the new list has a maximum length of \max (m,n) + 1max (m,n) +1.

2. Two numbers added

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.