Java O(n) solution easy


#1

public class Solution {
public ListNode addTwoNumbers(ListNode A, ListNode B) {
ListNode templist=null;
ListNode head=null;
int carry=0;
int value=0;
while(A!=null || B!=null){
if(A==null && B!=null){
if(B.val+carry>9){
carry=1;
value=0;
}else{
value=B.val+carry;
carry=0;
}
B=B.next;
}
else if(A!=null && B==null){
if(A.val+carry>9){
carry=1;
value=0;
}else{
value=A.val+carry;
carry=0;
}
A=A.next;
}else{
if(A.val+B.val+carry>9){
value=(A.val+B.val+carry)%10;
carry=1;
}else{
value=A.val+B.val+carry;
carry=0;
}
A=A.next;
B=B.next;
}
if(templist==null){
templist = new ListNode(value);
head=templist;
} else {
templist.next=new ListNode(value);
templist= templist.next;
}
if(A==null && B==null && carry==1){
templist.next=new ListNode(carry);
templist= templist.next;
}
}
return head;
}
}