Could any one help me


  • 0
    L

    class Solution {
    public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
    ListNode* res = new ListNode(0);
    ListNode* cur =res;

        int carry=0;
        while(l1||l2||carry){
            int sum=(l1 ? l1->val : 0) + (l2 ? l2->val : 0)+carry;
            carry=sum/10;
            int value=sum%10;
            cur->next=new ListNode(value);
            cur=cur->next;
    
            // l1=l1 ? l1->next : NULL;         //This is right 
            // l2=l2 ? l2->next : NULL;
    
            l1=l1->next ? l1->next : NULL; //while this is not right, cause Runtime Error, Why?
            l2=l2->next ? l2->next : NULL;  //What's wrong with this two lines compare to previous two?
        }
        return res->next;
        
    }
    

    };

    Is anything wrong with my


Log in to reply
 

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