Code Failing for large test cases ....please help me change it


#1

public class Solution {
ListNode R = null;
ListNode curr = null;
int rem = 0;

public ListNode addTwoNumbers(ListNode A, ListNode B) {
    ListNode x = A;
    ListNode y = B;
    int xval = 0;
    int yval = 0;
    int sum = 0;

    while(x!=null || y!=null){
         if(x == null){
             sum = y.val + rem;
         }
         else if(y == null){
             sum = x.val + rem;
         }
         
         else{
             sum = x.val + y.val + rem;
         }
         
        if(sum/10 > 0){
            updateResult( new ListNode(sum%10));
            rem = sum/10;
        }
        else{
            updateResult( new ListNode(sum));
        }
        
        if(x!=null){x = x.next;}
        if(y!=null){y = y.next;}
    }
    
    return R;    
    
}

public void updateResult(ListNode r){
    if(R == null){
        R = r;
        curr = r;
            }
    else{
       curr.next = r;
       curr = r;
    }
}

}