Easy to Understand C Solution


  • 1
    K
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
        struct ListNode* a = malloc(sizeof(struct ListNode)); // answer linked list with a head node
        a->next = NULL;
        struct ListNode* cur = a; // a pointer for answer list
        struct ListNode* tmp; // temp list node for adding
        int buff=0; // a variable for carried digit
    
        while(l1 || l2)  // while node is not null in either l1 or l2
        {
            tmp = malloc(sizeof(struct ListNode)); // creating a node
            tmp->next = NULL;
            
            if(l1 && !l2) // if only l1 element exsits on current position (when length of the lists is different)
            {
                int sum = l1->val+buff; // l1 value + carried digit
                tmp->val = sum%10; // assign sum value to temp list node
                cur->next = tmp; // add temp list node to answer linked list
                buff = sum/10; // put carried digit to buff variable if any
                l1 = l1->next; // move to next node in l1
            }
            
            else if(!l1 && l2) // if only l2 element exsits on current position (when length of the lists is different)
            {
                int sum = l2->val+buff; // l2 value + carried digit
                tmp->val = sum%10; // assign sum value to temp list node
                cur->next = tmp; // add temp list node to answer linked list
                buff = sum/10; // put carried digit to buff variable if any
                l2 = l2->next; // move to next node in l2
            }
            
            
            else // if l1 & l2 elements exist
            {
                int sum = l1->val + l2->val + buff; // l1 value + l2 value + carried digit
                
                tmp->val = sum%10; // assign sum value to temp list node
                buff = sum/10; // put carried digit to buff variable if any
                
                cur->next = tmp; // add temp list node to answer linked list
                l1 = l1->next; // move to next node in l1
                l2 = l2->next; // move to next node in l2
            }
            
            cur = cur->next; // move cur pointer to next node, which is last node in answer linked list
        } // end while
        
        /*
         *  if carried digit exists but l1 & l2 reached end of the list
         *    ex) l1 = {9, 9}, l2 = {1} -> {0, 0, 1} (last node)
         */
        if(buff != 0 && (!l1  && !l2)) {
            cur->next = malloc(sizeof(struct ListNode)); // create and add a node to end of the answer list
            cur->next->val = buff; // assign buff value to the node
            cur->next->next = NULL;
            return a->next; // return answer list, move to next one to exclude dummy head node
        }
        
        return a->next; // return answer list, move to next one to exclude dummy head node
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.