Two numbers together
Casual working
We are 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
Reason: 342 + 465 = 807
Problem-solving Code
/ * * * Definition for Singly-linked List. * Struct ListNode { * int Val; * struct ListNode * Next; *}; * / struct ListNode * addTwoNumbers ( struct ListNode * L1, struct ListNode * L2) { / * ideas: 1. At the same traverse two lists until two lists are empty until the 2. consider the carry issue when the carry two nodes + and carry on one of> 9 carry situ becomes (two numbers and + carry the on a) 10%, carry becomes two numbers on one and the carry +) / 10 consider boundary cases: 1. only a pointer to the next traversal list is not empty, it points to the next 2 . into the last bit position, so that when the carry is not an end loop * / struct ListNode * T1 = L1; // traverse the pointer does not change the original list struct* T2 = ListNode L2; struct ListNode * H3 = ( struct ListNode *) the malloc ( the sizeof ( struct ListNode)); // first node struct ListNode * PNEW = NULL; // points to the new node applications struct ListNode PTAIL = H3 * ; // points to the end of the list int SUM = 0 ; H3 -> Val = 0 ; H3 -> Next = NULL; // initialize the first node int with carry = 0 ; // forward one carry the while (T1 || || T2 with Carry) { PNEW = ( struct ListNode *) the malloc ( the sizeof ( struct ListNode)); // request a new node pNew-> Val = 0 ; PNEW -> Next = NULL; // apply for a new node, the node initialize int SUM = 0 ; // corresponding term and the IF (! T1 = NULL) // only if the pointer is not empty before traversal plus the value { SUM + = T1-> Val; } IF (! T2 = NULL) { SUM + T2- => Val; } SUM= +with Carry; IF (SUM> = 10 ) { SUM = SUM% 10 ; // modulo with Carry = . 1 ; // carry } the else { with Carry = 0 ; // cancel carry } PNEW -> Val = SUM; // new node into the list of results pTail-> Next = PNEW; // insertion end of the latest list PTAIL = PNEW; // move to the end node // downwardly traversing the list IF (T1 = NULL!) // not empty it points to the next node { T1 = T1-> Next; } IF (T2 =! NULL) { T2 = T2-> Next; } } struct ListNode * = temp2 of H3-> Next; Free (H3); // release blank header node return temp2 of; // return list }
Paint analysis
Ideas analysis
Overall, a total of two lists as shown, which are respectively the first node l1 and l2. Each node sequence list corresponding to the node is the head of the low numbers, and the corresponding tail node numbers high. Our aim is to position the two lists by adding, consider a carry case, end up with a new list, this list each bit corresponds to the result of the sum of two numbers corresponding to the list of results you.
As shown, L1 is from high to low 1984, l2 and 52 from high to low. The addition result of two numbers 2036, 2036 correspond exactly L3 from high to low.
Then we analyze the process, we start counting from the low to high, carry a carry.
* 0. initial carry is 0, carray = 0
* 4 + 2 + carry = 4 + 2 + 0 = 6 does not carry carry = 0;
* 8 + 5 + carry = 8 + 5 + 0 = 13 into a carry = 1; 10 = 3 is 13%
* 9 + 0 + carry = 9 + 0 + 1 = 10 into a carry = 1; 10 = 0 is 10%.
* 1 + 0 + carry = 1 + 0 + 1 = 2 binary 0
Code Analysis
1. For as simple as possible, we will create a new list to save the final result, and returns the head pointer of the list
2. Do not change the passed two lists throughout the process
3. We need to traverse two lists
- When it ends? Two conditions are indispensable
- Two lists are empty when
- Carry 0 time
4. apply every time a new node
- The new value of the node count how? If two nodes corresponding to the value of the carry value +> 9 to take the remainder, otherwise carry a value of two nodes +
- Carry how to consider? If two nodes corresponding to the value of the carry value of +> 1 9 carry, or carry 0
- Should also be considered a problem if traversing a linked list pointer points to a null, how to do? Only in traversing the pointer does not point to empty before adding value
5. Insert the new list, tail-interpolation, so as to correspond with the sequence.
6. traverse the pointer moves, only in the case of traversing the pointer is not null