You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
submission 1
try to solve this problem by convert the linked list into a int, caculate the sum and then convert it back to linked list.
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int i1 = toInt(l1);
int i2 = toInt(l2);
return toList(i1 + i2);
}
private ListNode toList(int i) {
ListNode head = new ListNode(0);
while (i > 0) {
ListNode newNode = new ListNode(i % 10);
i /= 10;
newNode.next = head;
head = newNode;
}
return reverse(head).next;
}
private int toInt(ListNode L) {
int res = 0;
int i = 0;
while (L != null) {
res = res + (int)(L.val * Math.pow(10, i));
i++;
L = L.next;
}
return res;
}
private ListNode reverse(ListNode head) {
if (head == null) {
return head;
}
ListNode prev = null;
while (head != null) {
ListNode next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
}
Input:
[0]
[0]
Output:
[]
Expected:
[0]
forgot to handle '0' in toList(int) method. if the sum is '0', it will return a null pointer.
submission 2
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int i1 = toInt(l1);
int i2 = toInt(l2);
return toList(i1 + i2);
}
private ListNode toList(int i) {
ListNode head = new ListNode(0);
if (i == 0) {
return head;
}
while (i > 0) {
ListNode newNode = new ListNode(i % 10);
i /= 10;
newNode.next = head;
head = newNode;
}
return reverse(head).next;
}
private int toInt(ListNode L) {
int res = 0;
int i = 0;
while (L != null) {
res = res + (int)(L.val * Math.pow(10, i));
i++;
L = L.next;
}
return res;
}
private ListNode reverse(ListNode head) {
if (head == null) {
return head;
}
ListNode prev = null;
while (head != null) {
ListNode next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
}
Input:
[9]
[1,9,9,9,9,9,9,9,9,9]
Output:
[]
Expected:
[0,0,0,0,0,0,0,0,0,0,1]
if the linked list is longer than the limit of int, then this method will round up.
submission 3
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
long i1 = toInt(l1);
long i2 = toInt(l2);
return toList(i1 + i2);
}
private ListNode toList(long i) {
ListNode head = new ListNode(0);
if (i == 0) {
return head;
}
while (i > 0) {
ListNode newNode = new ListNode((int)(i % 10));
i /= 10;
newNode.next = head;
head = newNode;
}
return reverse(head).next;
}
private long toInt(ListNode L) {
long res = 0;
long i = 0;
while (L != null) {
res = res + (long)(L.val * Math.pow(10, i));
i++;
L = L.next;
}
System.out.println(res);
return res;
}
private ListNode reverse(ListNode head) {
if (head == null) {
return head;
}
ListNode prev = null;
while (head != null) {
ListNode next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
}
Input:
[1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
[5,6,4]
Output:
[]
Expected:
[6,6,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]
changed all int into long, but still could not take input longer than long
submission 4
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode p = l1;
ListNode q = l2;
ListNode cur = dummy;
int carry = 0;
while (p != null || q != null) {
int x = (p == null) ? 0 : p.val;
int y = (q == null) ? 0 : q.val;
int sum = x + y + carry;
carry = sum / 10;
cur.next = new ListNode(sum % 10);
cur = cur.next;
if (p != null) {
p = p.next;
}
if (q != null) {
q = q.next;
}
}
if (carry == 1) {
cur.next = new ListNode(carry);
}
return dummy.next;
}
}