Whats wrong in this?


  • 0
    M
        /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     struct ListNode *next;
     * };
     */
    struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
        int carry = 0;
        for(; l1->next != NULL; l1 = l1->next, l2 = l2->next)
        {
            int temp = l1->val + l2->val + carry;
            if(temp > 9)
            {
                l1->val = temp - 10;
                carry = 1;
            }
            else
            {
                l1->val = temp;
                carry = 0;
            }
        }
        
        return l1;
    }

  • 0
    A

    what if l1 and l2 are different digit numbers ??


Log in to reply
 

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