Simple to understand c solution with malloc


  • 0
    S

    This solution creates two reversed linked list based on the given link list.
    The original link list is not modified. But the idea is still the reverse link list.

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
        struct ListNode* head;
        struct ListNode* l1_head;
        struct ListNode* l2_head;
        head = NULL;
        while(l1!=NULL){
            struct ListNode* temp;
            temp = (struct ListNode*)malloc(sizeof(struct ListNode));
            temp->val = l1->val;
            temp->next = head;
            head = temp;
            l1 = l1->next;
        }
        l1_head = head;
        head = NULL;
        while(l2!=NULL){
            struct ListNode* temp;
            temp = (struct ListNode*)malloc(sizeof(struct ListNode));
            temp->val = l2->val;
            temp->next = head;
            head = temp;
            l2=l2->next;
        }
        l2_head = head;
        
        
        int carry=0;
        head=NULL;
        while(l2_head!=NULL && l1_head!=NULL){
            struct ListNode* temp;
            temp = (struct ListNode*)malloc(sizeof(struct ListNode));
            temp->val = (l2_head->val)+(l1_head->val)+carry;
            if(temp->val>=10){
                temp->val = temp->val % 10;
                carry=1;
            }else{
                carry=0;
            }
            temp->next = head;
            head = temp;
            l2_head = l2_head->next;
            l1_head = l1_head->next;
        }
        
        if(l2_head!=NULL){
            while(l2_head!=NULL){
                struct ListNode* temp;
                temp = (struct ListNode*)malloc(sizeof(struct ListNode));
                temp->val = l2_head->val+carry;
                if(temp->val>=10){
                    temp->val = temp->val%10;
                    carry=1;
                }else{
                    carry=0;
                }
                temp->next = head;
                head = temp;
                l2_head = l2_head->next;
            }
        }
    
        if(l1_head!=NULL){
            while(l1_head!=NULL){
                struct ListNode* temp;
                temp = (struct ListNode*)malloc(sizeof(struct ListNode));
                temp->val = l1_head->val+carry;
                if(temp->val>=10){
                    temp->val = temp->val%10;
                    carry=1;
                }else{
                    carry=0;
                }
                temp->next = head;
                head = temp;
                l1_head = l1_head->next;
            }        
        }
        if(carry==1){
            struct ListNode* temp;
            temp = (struct ListNode*)malloc(sizeof(struct ListNode));
            temp->val = 1;
            temp->next = head;
            head = temp;
        }
        
        return head;
        
    }
    

Log in to reply
 

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