Reputation: 2237
I wish to save the starting point of a Node based Linked List ie. Linked List implemented using Nodes instead of the Java class, as I add more elements to the list and have to keep going to the next node to do so.
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int c = 0;
ListNode n = new ListNode(0);
ListNode l3 = n; //Node initialised to first node.
while (l1 != null || l2 != null || c != 0)
{
int sum = l1.val + l2.val + c;
c = sum/10;
sum = sum%10;
n = new ListNode(sum);
n = n.next;
l1 = l1.next;
l2 = l2.next;
}
return l3;
}
}
In the above example, I am using l3
to do so. But when I return l3
it is set to the last node in the list.
How can I prevent it from moving through the list with n
.
--------EDIT----------
Here is the question from leetcode for easier reference:
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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8
Upvotes: 2
Views: 612
Reputation: 5103
I had time to go back and clean this up a little. Hope it helps.
class Solution {
/**
* add two numbers in linked list form, lowest digit first form
* ie 1->0->3->4 represents the integer number 4301
* given 7->8->9->1 and 5->6->7->9 this method will return 2->5->7->1->1 (1987+9765=11752)
*
* @param lhs the left hand integer
* @param rhs the right hand integer
* @return the sum of the two integers
*/
public ListNode<Integer> addTwoNumbers(final ListNode<Integer> lhs,
final ListNode<Integer> rhs) {
return addTwoNumbers(lhs, rhs, 0);
}
private static ListNode<Integer> addTwoNumbers(final ListNode<Integer> lhs,
final ListNode<Integer> rhs,
final int carry) {
int sum = carry;
sum += lhs == null ? 0 : lhs.getValue();
sum += rhs == null ? 0 : rhs.getValue();
if (sum == 0 && rhs == null && lhs == null) {
return null;
} else {
return new ListNode<>(
addTwoNumbers(
lhs == null ? null : lhs.getNext(),
rhs == null ? null : rhs.getNext(),
sum / 10),
sum % 10);
}
}
public static void main(final String... args) {
final ListNode<Integer> lhs = ListNode.fromVarArgs(1, 9, 8, 7);
final ListNode<Integer> rhs = ListNode.fromVarArgs(9, 7, 6, 5);
System.out.print(lhs);
System.out.println("+");
System.out.println(rhs);
System.out.println("=");
System.out.println(new Solution().addTwoNumbers(lhs, rhs));
}
}
class ListNode<T> {
static <T> ListNode<T> fromVarArgs(T... digits) {
ListNode<T> ret = null;
for (final T digit : digits) {
ret = new ListNode<>(ret, digit);
}
return ret;
}
private ListNode<T> next;
final private T value;
ListNode(final ListNode<T> next, final T value) {
this.next = next;
this.value = value;
}
ListNode(final T value) {
this(null, value);
}
ListNode<T> getNext() {
return next;
}
void setNext(final ListNode<T> next) {
this.next = next;
}
T getValue() {
return value;
}
@Override
public String toString() {
if (getNext() == null) {
return getValue().toString();
} else {
return String.format(
"%s -> %s",
getValue().toString(),
getNext().toString());
}
}
}
Upvotes: 1
Reputation: 3902
Look at those lines
n = new ListNode(sum);
n = n.next;
here, n, the address of your current node, is set to a entirely different address. The node at the old address has now exactly nothing to do with the newly created node. In other words, every time you iterate through your loop, you throw away the old list and start another list. Of course, since the old nodes are not referenced by anything anymore, the garbage collector kills them. Only the first node that was created still exists, since l3 still points at it.
You need to manipulate the current node (instead of replacing it) and then link a new node to it, like this:
n.val = sum
if(l1.next != null || l2.next != null){
n.next = new ListNode(0);
n = n.next;
}
Btw., give your variables some proper names. Like l1 -> list1, n -> currentNode, c -> carry. Short is not good. Readable is good.
Also, the whole function will crash when the lists do not have the same length, since you always access the values of both when one of them could be null. You should go like
int sum = c;
if(l1 != null){sum+=l1.val;}
if(l2 != null){sum+=l2.val;}
at the beginning of the loop and
if(l1 != null){l1=l1.next;}
if(l2 != null){l2=l2.next;}
at the end.
Upvotes: 1