Add two numbers
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { if (l1 == null && l1 == null) { return null; } int digit = 0; int carry = 0; ListNode dummy = new ListNode(0); ListNode point = dummy; while (l1 != null && l2 != null) { digit = l1.val + l2.val + carry; ListNode curr = new ListNode(digit % 10); point.next = curr; carry =digit / 10; l1 = l1.next; l2 = l2.next; point = point.next; } while (l1 != null) { digit = l1.val + carry; point.next = new ListNode(digit % 10); carry = digit / 10; l1 = l1.next; point = point.next; } while (l2 != null) { digit = l2.val + carry; point.next = new ListNode(digit % 10); carry = digit / 10; l2 = l2.next; point = point.next; } if (carry != 0) { point.next = new ListNode(carry); } return dummy.next; } }