Uri has a good answer Uri has a good answer, this answer is just to add a second spin on the problem....
Recursion is a good tool to have, but it is not necessarily the best tool for all occasions. In this instance, it's a toss-up.... but the iterative solution to this problem is perhaps a simpler thing to read..... and should be considered. Additionally, in many cases an iterative solution will outperform a recursive solution. In this case, again, it is a toss-up because the lists will be so short.... but, if the list was long, the recursive approach will fail with a stack-overflow... the iterative approach will keep trucking though.
public static final LinkedListNode add(LinkedListNode a, LinkedListNode b) {
// this pointer points to the result, it is not the actual result.
final LinkedListNode pointer = new LinkedListNode(0);
int carry = 0;
LinkedListNode cursor = pointer;
while (a != null || b != null) {
int digitsum = carry;
if (a != null) {
digitsum += a.value;
a = a.next;
}
if (b != null) {
digitsum += b.value;
b = b.next;
}
cursor.next = new LinkedListNode(digitsum % 10);
carry = digitsum / 10;
cursor = cursor.next;
}
if (carry != 0) {
cursor.next = new LinkedListNode(carry);
}
// don't return the dummy pointer, return the actual result
return pointer.next;
}
Some side-notes:
- the variables
l1
andl2
are not great names.... - The
LinkedListNode
should probably have afinal
value, and there should be getters for the value and next, and a setter for the next.
Uri has a good answer, this answer is just to add a second spin on the problem....
Recursion is a good tool to have, but it is not necessarily the best tool for all occasions. In this instance, it's a toss-up.... but the iterative solution to this problem is perhaps a simpler thing to read..... and should be considered. Additionally, in many cases an iterative solution will outperform a recursive solution. In this case, again, it is a toss-up because the lists will be so short.... but, if the list was long, the recursive approach will fail with a stack-overflow... the iterative approach will keep trucking though.
public static final LinkedListNode add(LinkedListNode a, LinkedListNode b) {
// this pointer points to the result, it is not the actual result.
final LinkedListNode pointer = new LinkedListNode(0);
int carry = 0;
LinkedListNode cursor = pointer;
while (a != null || b != null) {
int digitsum = carry;
if (a != null) {
digitsum += a.value;
a = a.next;
}
if (b != null) {
digitsum += b.value;
b = b.next;
}
cursor.next = new LinkedListNode(digitsum % 10);
carry = digitsum / 10;
cursor = cursor.next;
}
if (carry != 0) {
cursor.next = new LinkedListNode(carry);
}
// don't return the dummy pointer, return the actual result
return pointer.next;
}
Some side-notes:
- the variables
l1
andl2
are not great names.... - The
LinkedListNode
should probably have afinal
value, and there should be getters for the value and next, and a setter for the next.
Uri has a good answer, this answer is just to add a second spin on the problem....
Recursion is a good tool to have, but it is not necessarily the best tool for all occasions. In this instance, it's a toss-up.... but the iterative solution to this problem is perhaps a simpler thing to read..... and should be considered. Additionally, in many cases an iterative solution will outperform a recursive solution. In this case, again, it is a toss-up because the lists will be so short.... but, if the list was long, the recursive approach will fail with a stack-overflow... the iterative approach will keep trucking though.
public static final LinkedListNode add(LinkedListNode a, LinkedListNode b) {
// this pointer points to the result, it is not the actual result.
final LinkedListNode pointer = new LinkedListNode(0);
int carry = 0;
LinkedListNode cursor = pointer;
while (a != null || b != null) {
int digitsum = carry;
if (a != null) {
digitsum += a.value;
a = a.next;
}
if (b != null) {
digitsum += b.value;
b = b.next;
}
cursor.next = new LinkedListNode(digitsum % 10);
carry = digitsum / 10;
cursor = cursor.next;
}
if (carry != 0) {
cursor.next = new LinkedListNode(carry);
}
// don't return the dummy pointer, return the actual result
return pointer.next;
}
Some side-notes:
- the variables
l1
andl2
are not great names.... - The
LinkedListNode
should probably have afinal
value, and there should be getters for the value and next, and a setter for the next.
Uri has a good answer, this answer is just to add a second spin on the problem....
Recursion is a good tool to have, but it is not necessarily the best tool for all occasions. In this instance, it's a toss-up.... but the iterative solution to this problem is perhaps a simpler thing to read..... and should be considered. Additionally, in many cases an iterative solution will outperform a recursive solution. In this case, again, it is a toss-up because the lists will be so short.... but, if the list was long, the recursive approach will fail with a stack-overflow... the iterative approach will keep trucking though.
public static final LinkedListNode add(LinkedListNode a, LinkedListNode b) {
// this pointer points to the result, it is not the actual result.
final LinkedListNode pointer = new LinkedListNode(0);
int carry = 0;
LinkedListNode cursor = pointer;
while (a != null || b != null) {
int digitsum = carry;
if (a != null) {
digitsum += a.value;
a = a.next;
}
if (b != null) {
digitsum += b.value;
b = b.next;
}
cursor.next = new LinkedListNode(digitsum % 10);
carry = digitsum / 10;
cursor = cursor.next;
}
if (carry != 0) {
cursor.next = new LinkedListNode(carry);
}
// don't return the dummy pointer, return the actual result
return pointer.next;
}
Some side-notes:
- the variables
l1
andl2
are not great names.... - The
LinkedListNode
should probably have afinal
value, and there should be getters for the value and next, and a setter for the next.