What is wrong with my solution?


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

    for [5] and [5] my solution produces [1,0] instead of [0,1]


  • 0
    E

    which stupid downvoted this, and why would you do that? what is wrong with you? this is just a question. ))


Log in to reply
 

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