Leetcode 2 Add Numbers

Source: Internet
Author: User

/**
* Id:2
* Name:add-Numbers
* Data structure:linked List
* Time Complexity:
* Space Complexity:
* Tag:linklist
* Difficult:medium

* Problem:
* You are given, linked lists representing, and non-negative numbers.
* The digits is stored in reverse order and all of their nodes contain a single digit.
* Add the numbers and return it as a linked list.
* Input: (2, 4, 3) + (5, 6, 4)
* 8 Output:7, 0

Idea one: main note who first traverse the end, and pay attention to the problem of rounding, carry, is when there is no new node, but there are carry, it is necessary to add a new node in.

In the two lists are not all finished, add, carry, and then merge the L2 into the L1 inside, and then let the L1 go through, complete all the carry operations.

Idea two: Create a brand new list, then add each value to the L1,l2,carry each time, and finally return a new list.

1 classSolution {2  Public:3ListNode *addtwonumbers (ListNode *l1, ListNode *L2) {4         if(L1 = =NULL)5             returnL2;6         if(L2 = =NULL)7             returnL1;8 9ListNode *fakenode1 =NewListNode (0);TenListNode *fakenode2 =NewListNode (0); OneFakeNode1-Next =L1; AFakeNode2-Next =L2; -ListNode *P1 =FakeNode1; -ListNode *P2 =FakeNode2; the         intcarry =0; -         intsum =0; -  -          while(P1->next && p2->next) +         { -P1 = p1->Next; +P2 = p2->Next; Asum = carry + P1->val + p2->Val; atcarry = sum/Ten; -P1->val = sum%Ten;  -               -         } -          -         if(P1->next==null && p2->next!=NULL) in         { -P1->next = p2->Next; to         } +         DeleteP2; -         DeleteFakeNode2; the          while(p1->next) *         { $P1 = p1->Next;Panax Notoginsengsum = carry + p1->Val; -carry = sum/Ten; theP1->val = sum%Ten; +         } A        if(carry!=0) the         { +ListNode *last =NewListNode (carry); -P1->next =Last ;  $         }   $         returnFakenode1->Next; -     } -};

Idea two:

1 classSolution {2  Public:3ListNode *addtwonumbers (ListNode *l1, ListNode *L2) {4ListNode *dummy =NewListNode (0), *p =dummy;5         intcarry =0;6          while(L1 | | l2 | |carry) {7             if(L1) {8Carry+=l1->Val;9L1 = l1->Next;Ten             } One             if(L2) { ACarry+=l2->Val; -L2 = l2->Next; -             } theP->next =NewListNode (carry%Ten);//every time create a new node. -Carry/=Ten; -p = p->Next; -         } +         returnDummy->Next; -     } +};

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