/**
* Definition for singly-linked list.
* public class ListNode
{
* int val;
* ListNode next;
* ListNode(int x) {
*
val = x;
* next = null;
* }
* }
*/
public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode head = new ListNode(-1); ListNode p = head; int carry = 0; while(l1 != null || l2 != null){ int result = 0; int va = (l1 == null)? 0: l1.val; int vb = (l2 == null)? 0: l2.val; result = (va+vb+carry)%10; carry = (va+vb+carry)/10; p.next = new ListNode(result); p = p.next; l1 = (l1 == null ? null : l1.next); l2 = (l2 == null ? null : l2.next); } if(carry != 0){ p.next = new ListNode(carry); } return head.next; } }