# my ugly codes, but easy to understand

• ``````/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode pre = new ListNode(-1);
ListNode helper = pre;
ListNode p1 = l1;
ListNode p2 = l2;
int c = 0;
int num = 0;
while(p1!=null && p2!=null){
int temp = p1.val + p2.val + c;
c = temp/10;
num = temp%10;
if(c==0){
ListNode n = new ListNode(temp);
helper.next = n;
helper = helper.next;
}else{
ListNode n = new ListNode(num);
helper.next = n;
helper = helper.next;
if(p1.next == null && p2.next == null){
ListNode m = new ListNode(c);
helper.next = m;
}
}
p1 = p1.next;
p2 = p2.next;
}
while(p1 ==null && p2!=null){
int tmp = p2.val + c;
if(tmp <=9){
p2.val = tmp;
helper.next = p2;
break;
}else{
c = tmp/10;
num = tmp%10;
ListNode n = new ListNode(num);
helper.next = n;
helper = helper.next;
if(p2.next == null){
ListNode m = new ListNode(c);
helper.next = m;
}
}
p2 = p2.next;
}
while(p2 ==null && p1!=null){
System.out.println(c);
int tmp = p1.val + c;
if(tmp <=9){
p1.val = tmp;
helper.next = p1;
break;
}else{
c = tmp/10;
num = tmp%10;
ListNode n = new ListNode(num);
helper.next = n;
helper = helper.next;
if(p1.next == null){
ListNode m = new ListNode(c);
helper.next = m;
}
}
p1 = p1.next;
}
return pre.next;
}
}
``````

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