T2_ two numbers together

Method 1: simultaneously traverse two lists, one simultaneously adding two lists, it is added more than 10 set next addition.

/ * * 
 . * Definition for Singly-linked List 
 * struct ListNode { 
 * int Val; 
 * ListNode * Next; 
 * ListNode (int X): Val (X), Next (NULL) {} 
 *}; 
 * / 
class Solution {
 public : 
    ListNode * addTwoNumbers (L1 * ListNode, ListNode * L2) { 
        ListNode * P = new new ListNode (- . 1 ); // create a node 
        ListNode * H = P; // pointer 
        int SUM = 0 ; // two numbers adding 
        BOOL with Carry = to false ; //Determining whether the required intake. 1 
        the while (L1 = L2 = NULL ||!! NULL) { 
               SUM = 0 ;
             IF (! L1 = NULL) { 
                SUM + = L1-> Val; // traverse the list L1 
                L1 = L1-> Next ; 
            } 
            
            IF (L2 =! NULL) { 
                SUM + = L2-> Val; // traverse the list L2 
                L2 = L2-> Next; 
            } 
            IF (with Carry) { 
                SUM ++; // with Carry a first pair of adders is false; a second pair of adders determined only need 
            }
            // SUM = SUM> SUM-10 = 10:? SUM;
            
            H -> Next = new new ListNode (SUM% 10 ); 
            H = H-> Next; 
            with Carry = SUM> = 10 ? to true : to false ; 

        } 
        IF (with Carry) { 
            H -> Next = new new ListNode ( . 1 ); 
        } 
        return p-> next; // first node value of -1 ,, it is necessary to start next 
        
       
    } 
};

Method 2:

Guess you like

Origin www.cnblogs.com/tianjiale/p/10975345.html