my code run in xcode ok,but ...


  • 0
    Z

    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {

    struct ListNode *node_print = l1;
    printf("\n l1:");
    do  {
        printf("%i",node_print->val);
        node_print= node_print->next;
        if (node_print) {
            printf("->");
        }
    }while (node_print!=NULL);
    node_print = l2;
    printf("\n l2:");
    do  {
        printf("%i",node_print->val);
        node_print= node_print->next;
        if (node_print) {
            printf("->");
        }
    }while (node_print!=NULL);
    printf("\n\r");
    
    
    struct ListNode *result = (struct ListNode *)malloc(sizeof(struct ListNode));
    result->next = NULL;
    node_print = result;
    
    
    result->val = l1->val+l2->val;
    if (result->val >=10) {
        result->next = (struct ListNode *)malloc(sizeof(struct ListNode));
        result->next->next = NULL;
    
        result->next->val = result->val/10;
        result->val = result->val%10;
        
    }
    
    l1 = l1->next;
    l2 = l2->next;
    while (l2!=NULL || l1!= NULL) {
        if (result->next) {
            result = result->next;
        }else{
            result->next = (struct ListNode *)malloc(sizeof(struct ListNode));
            result->next->next = NULL;
    
            result = result->next;
    
        }
        
        if (l1 == NULL) {
            result->val+= l2->val;
            if (result->val >=10) {
                result->next = (struct ListNode *)malloc(sizeof(struct ListNode));
                result->next->next = NULL;
    
                result->next->val = result->val/10;
                result->val = result->val%10;
            }
            
            
        }else if (l2 == NULL) {
            result->val+= l1->val;
            if (result->val >=10) {
                result->next = (struct ListNode *)malloc(sizeof(struct ListNode));
                result->next->next = NULL;
    
                result->next->val = result->val/10;
                result->val = result->val%10;
            }
        }else if (l1!=NULL && l2!=NULL ) {
            result->val+= l1->val+l2->val;
            if (result->val >=10) {
                result->next = (struct ListNode *)malloc(sizeof(struct ListNode));
                result->next->next = NULL;
    
                result->next->val = result->val/10;
                result->val = result->val%10;
            }
    
        }
    
        if (l1) {
            l1 = l1->next;
    
        }
        if (l2) {
            l2 = l2->next;
    
        }
        
    }
    /*
     printf("\n result:");
     do  {
     printf("%i",node_print->val);
     node_print= node_print->next;
     if (node_print) {
     printf("->");
     }
     }while (node_print!=NULL);
     printf("\n");
     */
    return node_print;
    

    }


  • 0
    Z

    0_1490611140170_upload-54671d18-e45e-4367-b71e-9ef918cf9aae


Log in to reply
 

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