2, ADD, Numbers

Source: Internet
Author: User

1, Add two numbers--This is the second question of Leedcode:
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

The number in the given two list,list is stored in reverse order, and each bit contains a number. Adds two lists and returns a list as the result. The logic for adding the two lists is as follows: Each bit is added, every ten in one.

Solution Ideas:
1, first of all, this is the addition of two linked list problem. Because there is no mention of the list who is long who is short, so the linked list of the corresponding position added to the results linked list, and then the long list is directly assigned to the results linked list.
2, because the number of each position can only be 0-9 of the number, so will produce a rounding. So when the list is added, it needs to add the number of digits from the front.
3, if the final addition of all the linked list position, there is a carry system, at this time this carry alone produces a node.

PS: Actually is two number of addition operation, example of the input (reverse order) is actually 342+465 calculation.
We decompose the steps in the calculation:
2+5=7;
4+6=10, go in one, get 0.
2+5+1 (rounding) = 8;
So you get 807, and then you reverse the 708.

The boundary conditions need to be noted here, for example: input is [1] and [9,9].

The code for the accept is posted here:

Java version

/** * Definition for singly-linked list. * public class ListNode {* int val; * ListNode Next; * ListNode ( int x) {val = x;} *} * /Public class solution {Public ListNode addtwonumbers (listnode L1, ListNode L2) {ListNode L3 =NewListNode (0); ListNode result =NewListNode (-1); int carry =0; while(L1! =NULL&& L2! =NULL) {intVal= L1.Val+ L2.Val+ Carry; carry =0;if(Val>=Ten) {carry =1; } listnode node =NewListNode (Val%Ten);if(result.)Val== -1) {result.Val=0;            Result.next = node;            } l3.next = node;            L3 = node;            L1 = L1.next;        L2 = L2.next; } while(L1! =NULL) {intVal= L1.Val+ Carry; carry =0;if(Val>=Ten) {carry =1; } listnode node =NewListNode (Val%Ten);            L3.next = node;            L3 = node;        L1 = L1.next; } while(L2! =NULL) {intVal= L2.Val+ Carry; carry =0;if(Val>=Ten) {carry =1; } listnode node =NewListNode (Val%Ten);            L3.next = node;            L3 = node;        L2 = L2.next; }if(Carry >0) {ListNode node =NewListNode (Carry%Ten);            L3.next = node;        L3 = node; } result = Result.next;returnResult }}

C + + version:

/** * Definition forsingly-linked list. * struct ListNode {*intVal * ListNode *Next; * ListNode (intx): Val (x),Next(NULL) {} * }; */classSolution { Public: listnode* addtwonumbers (listnode* L1, listnode* L2) {listnode* start=NULL; listnode* L3 =NULL;intcarry =0; while(L1! =NULL&&l2! =NULL)         {intValue = L1->val + L2->val + carry; carry =0;if(Value >=Ten) {carry =1; } listnode* node =NewListNode (value%Ten);if(Start = =NULL&&l3 = =NULL) {start = node;            L3 = node; }Else{l3->Next= node; L3 = l3->Next; } L1 = l1->Next; L2 = l2->Next; } while(L1! =NULL) {intValue = L1->val + carry; carry =0;if(Value >=Ten) {carry =1; } listnode* node =NewListNode (value%Ten);if(Start = =NULL&&l3 = =NULL) {start = node;            L3 = node; }Else{l3->Next= node; L3 = l3->Next; } L1 = l1->Next; } while(L2! =NULL) {intValue = L2->val + carry; carry =0;if(Value >=Ten) {carry =1; } listnode* node =NewListNode (value%Ten);if(Start = =NULL&&l3 = =NULL) {start = node;            L3 = node; }Else{l3->Next= node; L3 = l3->Next; } L2 = L2->Next; }if(Carry >0) {listnode* node =NewListNode (Carry%Ten); carry =0;if(Start = =NULL&&l3 = =NULL) {start = node;            L3 = node; }Else{l3->Next= node; L3 = l3->Next;    }} return start; }};

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.