-
Stem that: given two non-empty list is used to represent two non-negative integer. Where their respective bits are stored in reverse order of the way, and they each node can store only one digit. If we add up these two numbers, it will return a new list and to represent them. You can assume that in addition to the numbers 0, these two numbers will not begin with 0.
-
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Cause: 342 + 465 = 807
-
Source: stay button (LeetCode)
- Difficulty: Moderate
- Title list given structure
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/
- Outline of Solution: The general idea of this problem is to separately through each digit of the two lists, these two lists l1, l2 corresponds to the median of the addition calculation value is stored in the linked list to the newly opened after the calculation, then the pointer It refers to the next digit to continue operations. At this point, it is easy to think of a few questions: 1. If the length of these two lists are not the same as how to do? 2. The need to carry more than 10 how to do if the corresponding bits are added?
- Implementation: I would like to see specific ideas, and now you want to select the method used, first of all, we need to open a new list, while the need to traverse l1, l2 list, but the list of unknown length, so we need to use a while loop to a node by node traversal, the end of the cycle when it? That depends on how 2 solve two problems. Meanwhile, in order to solve the first problem, we can use the statement to a judge, if there is a node l1, then put together its value, if there is a node l2, then put together its value. For the second problem, we can set a carry flag add = 0, if l1-> val + l2-> val> = 10, then add the carry flag is set to 1, and assignment to the next node list as a new beginning value, l1-> val + l2-> val-10 assigned to the current new list. In the after want to end the specific operational procedures, we look at input and output, will find that a problem that we write a new list, with a head node unused. So when the final output should be deleted.
- The following source code:
-
. 1 / * * 2 . * Definition for Singly-linked List . 3 * struct ListNode { . 4 * int Val; . 5 * ListNode * Next; . 6 * ListNode (int X): Val (X), Next (NULL) {} . 7 * }; . 8 * / . 9 class Solution { 10 public : . 11 ListNode addTwoNumbers * (L1 * ListNode, ListNode * L2) { 12 is ListNode * Result = new new ListNode ( 0 ), R & lt * = Result; // define a new list 13 is int = the Add 0 ; // define a carry flag 14 the while (!! = NULL || L2 L1 = NULL) {// If any of a list traversal is not complete 15 R-> Next = new new ListNode (the Add); // advance assigned to carry the newly opened list 16 R & lt = R-> Next; . 17 18 is IF (= l1! NULL) {// If l1 is not traversed . 19 R-> Val = R-> Val + L1-> Val; 20 is l1 = L1-> Next; 21 is } 22 is IF (l2 =! NULL) {// If no traversal l2 End 23 is R-> Val = R-> Val + L2-> Val; 24 l2 = L2-> Next; 25 } 26 is 27 IF (R-> Val> = 10 ) {// if there is a carry 28 R-> Val = R-> Val- 10 ; 29 the Add = . 1 ; 30 } 31 is the else {// if no carry 32 the Add = 0 ; 33 is } 34 is } 35 IF (the Add> 0 ) {// Finally, if the final carry bit 36 R-> Next = new new ListNode (the Add); 37 [ } 38 is return result-> Next; // note is not a return lead linked list of nodes 39 } 40 };
- Summary: This question is not difficult, mainly due to the use of inspection list.