Skip to content

Instantly share code, notes, and snippets.

@13andrew13
Created November 14, 2017 16:53
Show Gist options
  • Save 13andrew13/4294e06f708d7ddb2cfb93d9a4bae971 to your computer and use it in GitHub Desktop.
Save 13andrew13/4294e06f708d7ddb2cfb93d9a4bae971 to your computer and use it in GitHub Desktop.
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int sum = 0;
ListNode res;
if(l1 == null && l2 != null){
l1 = new ListNode(0);
} else if(l1 != null && l2 == null){
l2= new ListNode(0);
}else if(l1 == null && l2 == null){
return null;
}
sum = sum + l1.val + l2.val;
res = new ListNode(sum%10);
if(sum >= 10){
if (l2.next!=null) {
l2.next.val += 1;
} else if (l1.next!=null) {
l1.next.val+=1;
}
else{
l1.next = new ListNode(0);
l1.next.val +=1;
}
}
res.next = addTwoNumbers(l1.next, l2.next);
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment