Simple two-count and re-enter << Add-Numbers >>

Source: Internet
Author: User

Please see the title Description :

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)
Output:7, 0, 8

1 //definition For singly-linked list.
2     class ListNode 3     { 4public         int val; 5          Public ListNode Next; 6          Public ListNode (int x) 7          8              9         }
Ten   }

Analysis:
This two number is not difficult to add, just two positive integers are placed in the list. Another point is that every number of integers is spared to store, then this significantly reduced the difficulty, realized like our primary school when the row tree calculation two number of the and, from the low to add, and with 10 of the remainder in the standard, and with 10 of the quotient produces high-level carry. In this case, only the table header of the two linked lists is added, and the corresponding position of the linked list holding the result of the calculation holds the integer and the sum of the two linked list and the remainder of the number divided by 10, and holds the carry with the temporary variable for adding the sum of the next two integers. Until the list of two linked lists has been traversed, the resulting list is out. The numbers of the integers in the resulting list are also reversed.

From the previous analysis, we can use both recursive and non-recursive algorithms to implement.
The C # Reference is implemented as follows:  
Recursive implementations:
1         PublicListNode addtwonumbers (listnode L1, ListNode L2)2    {3             returnAddTwoNumbers (L1, L2,0);4    }
5 PublicListNode addtwonumbers (listnode L1, ListNode L2,intcarry)6 {7 if(L1 = =NULL&& L2 = =NULL&& carry = =0)8 {9 return NULL;Ten } OneListNode result =NewListNode (0); A intsum = (L1 = =NULL?0: l1.val) + (L2 = =NULL?0: l2.val) +carry;
-Result.val = sum%Ten; -ListNode more = addtwonumbers (L1 = =NULL?NULL: L1.next, theL2 = =NULL?NULL: L2.next, -Sum >=Ten?1:0 ); -Result.next =more ; - returnresult; +}
 
Non-recursive implementations:
1   PublicListNode AddTwoNumbers2 (listnode L1, ListNode L2)2 {3ListNode node =NewListNode (0);4ListNode result =node;5             6              intcarry =0;7               while(L1! =NULL|| L2! =NULL|| Carry! =0)8       {9                  intsum = (L1 = =NULL?0: l1.val) + (L2 = =NULL?0: l2.val) +carry;TenL1 = (L1 = =NULL?NULL: L1.next); OneL2 = (L2 = =NULL?NULL: L2.next); A  -Node.next =NewListNode (sum%Ten ); -node =Node.next; the  -carry = SUM/Ten; -        } -              returnResult.next; +}
 
Summary: This topic is not difficult as long as the quiet heart to come out, the details of the problems involved are not many. So if you change the title, and change it into two linked lists, each digit that holds a positive integer is stored in a positive direction, and how is it implemented? Note: The linked list is one-way. It is important to note the length of the two integers. We can do this: a) compare the length of the two linked lists with 0 short list  b) recursive results to the first
  

Simple two-count and re-enter << 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.