Why it showing NullPointerException? please help.


  • 0
    T

    ...
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    ListNode temp=new ListNode(0);
    ListNode head=temp;
    int carry=0;
    int sum=0;
    while(l1!=null||l2!=null||carry!=0){
    if(l1!=null&&l2!=null)
    sum=carry+l1.val+l2.val;
    else if(l1!=null)
    sum=carry+l1.val;
    else if(l2!=null)
    sum=carry+l2.val;
    else
    sum=carry;
    carry=sum/10;
    temp.val=sum-carry*10;
    l1=l1.next;
    l2=l2.next;
    if(l1!=null||l2!=null||carry!=0){
    temp=temp.next;
    temp=new ListNode(0);
    }
    }
    return head;
    }

    ...


Log in to reply
 

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