Topic:
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
Ideas:
Add from left to right to record the carry value.
Note: The two lists are different lengths, and the list is added to check if the rounding is 0.
/** Definition for singly-linked list. * Function ListNode (val) {* This.val = val; * This.next = null; *}
*//** * @param {listnode} L1 * @param {listnode} L2 * @return {ListNode}*/varAddTwoNumbers =function(L1, L2) {varCarrybit=0,temphead=NewListNode (0); varp=Temphead; while(l1&&L2) {L1.val+=l2.val+Carrybit; Carrybit=L1.VAL/10; L1.val=l1.val%10; P.next=L1; P=P.next; L1=L1.next; L2=L2.next; } varLp= (l1==NULL?l2:l1); while(LP) {if(carrybit==0) {P.next=LP; Break; }Else{lp.val+=Carrybit; Carrybit=LP.VAL/10; Lp.val=lp.val%10; P.next=LP; P=P.next; } LP=Lp.next; } if(carrybit!=0) {P.next=NewListNode (carrybit); } returnTemphead.next;};
If the list stores integers, the high-level is in front of what to do with it.
1, the simplest idea is that you can first reverse the list, then call the above algorithm, and finally reverse the results.
2, can it be handled from high to low direction? We know that the carry is from the low to the high, if from the high to the low direction, when the calculation to a certain need to carry, there is no way to know that the carry passed to the previous one? Here are a few examples:
From the highest to the lowest we remember in turn the first 1,2...,7. The position of the red marker in the figure is: 1 of the placed seats for the next time when rounding is required
The 1th bit is added, the result is 12, the rounding is required, the carry is placed to the No. 0 bit, and the next carry flag is marked with the 1th digit.
The 2nd bit is added, the result is 3, no rounding is required; Mark 2nd Digit is the next carry flag
The 3rd bit is added, the result is 3, no rounding is required; Mark 3rd Digit is the next carry flag
The 4th bit is added, the result is 9, no rounding is required, and the carry flag does not need to be moved, because 9 plus a rounding to continue the forward rounding
The 5th bit is added, the result is 9, no rounding is required, and the carry flag does not need to be moved, because 9 plus a rounding to continue the forward rounding
The 6th bit is added, the result is 14, the rounding is required, the carry is placed on the 3rd bit of the preceding mark, and the 4th and 5th positions are 0, and the 6th bit is the next rounding flag.
7th place Ibid.
So to sum up, from high to low to calculate the addition, the rule is:
First, if the current bit does not carry: (1) If the current bit and less than 9, then the change is set to the next carrying flag (2) if and equal to 9, the rounding flag is unchanged
Second, if the current bit has carry: add 1 to the front of the flag of the seat, while the flag and the current bit between the position of all 0 (because the bits between them are definitely all 9), the current bit is set to carry flag
The two addend of the above example have the same length, and if the lengths are not the same, the earlier parts of the long integers are processed first.
We reverse the input of the Leetcode and test the code as follows:
/** Definition for singly-linked list. * struct ListNode {* int val; * ListNode *next; * ListNode (int x) : Val (x), Next (NULL) {}}; */classSolution { Public: ListNode*addtwonumbers (ListNode *l1, ListNode *L2) {L1=reverselist (L1); L2=Reverselist (L2); intN1 = lenlist (L1), N2 =Lenlist (L2); if(N1 < N2)//L1 pointing to a longer list{swap (N1,N2); Swap (L1,L2); } //Carryloc is the next occurrence of rounding, the 1 of the carry will be placed, the pre points to the last node of the current result list//P1,P2 is the currently processed L1,L2 node, respectively//because the highest bit of addend is likely to carry, a new node is added NewheadListNode *newhead =NewListNode (0), *carryloc = newhead, *pre = newhead, *p1 =L1; for(inti =0; i < n1-n2; i++)//to handle the portion of a L1 high-growing { if(P1->val <9) Carryloc =P1; Pre->next =P1; Pre=P1; P1= p1->Next; } ListNode* P2 =L2; while(P1! =NULL) {Pre->next =P1; Pre=P1; P1->val + = p2->Val; if(P1->val >9) {Carryloc->val + =1; for(Carryloc = carryloc->next; carryloc! = P1; carryloc = carryloc->next)//Carryloc to P1 node all 0Carryloc->val =0; P1->val-=Ten; } if(P1->val <9) Carryloc=P1; P1= p1->Next; P2= p2->Next; } if(Newhead->val! =0)returnreverselist (Newhead); Else returnReverselist (newhead->next); } //Reverse Linked listListNode *reverselist (ListNode *L1) {ListNode*p = l1->next, *pre =L1; L1->next =NULL; while(p) {ListNode*tmp = p->Next; P->next =Pre; Pre=p; P=tmp; } returnPre; } //To find the chain table length intLenlist (ListNode *head) { intres =0; while(head) {res++; Head= head->Next; } returnRes; }};
Back from:http://www.cnblogs.com/TenosDoIt/p/3735362.html
"Linked list" Add Numbers