'Add Two Numbers using Linked List printSum() not working
I am trying to write a main to print the result of adding two numbers from a class I created called ListNode. I tried to create a main and was getting errors. I have created objects and I am not sure if I am doing this correctly. I would like to display the result. I am getting infinite loop error still.
package LinkedList;
import java.util.LinkedList;
import java.util.*;
public class ListNode{
int value;
ListNode next;
ListNode(){};
ListNode(int value){
this.value = value;
}
ListNode(int value, ListNode next){
this.value = value;
this.next = next;
}
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy_head = new ListNode(0);
ListNode list1 = l1;
ListNode list2 = l2;
ListNode result = dummy_head;
int carry = 0;
while(list1 != null || list2 != null) {
int x = 0;
int y = 0;
int sum = x + y + carry;
if(list1 != null) {
x = list1.value;
}
else {
x = 0;
}
if(list2 != null) {
y = list2.value;
}
else {
y = 0;
}
carry = sum / 10;
result.next = new ListNode(sum % 10);
result = result.next;
if(list1 != null) {
list1 = list1.next;
}
if(list2 != null) {
list2 = list2.next;
}
}
if(carry > 0) {
result.next = new ListNode(carry);
}
return dummy_head.next;
}
public static void printSum(ListNode l1, ListNode l2) {
ListNode result = ListNode.addTwoNumbers(l1, l2);
while(result.next != null) {
System.out.println(result.value);
}
}
public static void main (String args[]) {
ListNode lst1 = new ListNode();
lst1.value = 3;
lst1.next = new ListNode();
lst1.next.value = 2;
lst1.next.next = new ListNode();
lst1.next.next.value = 1;
ListNode lst2 = new ListNode();
lst2.value = 3;
lst2.next = new ListNode();
lst2.next.value = 2;
lst2.next.next = new ListNode();
lst2.next.next.value = 1;
ListNode.printSum(lst1, lst2);
}
}
Solution 1:[1]
I don't fully understand the algorithm you're trying to implement, however the following is my best guess:
package LinkedList;
public class ListNode {
int value;
ListNode next;
ListNode() {};
ListNode(int value) {
this.value = value;
}
ListNode(int value, ListNode next) {
this.value = value;
this.next = next;
}
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy_head = new ListNode(0);
ListNode list1 = l1;
ListNode list2 = l2;
ListNode result = dummy_head;
int carry = 0;
while (list1 != null || list2 != null) {
int x = 0;
int y = 0;
if (list1 != null)
x = list1.value;
if (list2 != null)
y = list2.value;
int sum = x + y + carry;
carry = sum / 10;
result.next = new ListNode(sum % 10);
result = result.next;
if (list1 != null)
list1 = list1.next;
if (list2 != null)
list2 = list2.next;
}
if (carry > 0)
result.next = new ListNode(carry);
return dummy_head.next;
}
public static void printSum(ListNode l1, ListNode l2) {
ListNode result = ListNode.addTwoNumbers(l1, l2);
while (result != null) {
System.out.println(result.value);
result = result.next;
}
}
public static void main(String args[]) {
ListNode lst1 = new ListNode();
lst1.value = 3;
lst1.next = new ListNode();
lst1.next.value = 2;
lst1.next.next = new ListNode();
lst1.next.next.value = 1;
ListNode lst2 = new ListNode();
lst2.value = 3;
lst2.next = new ListNode();
lst2.next.value = 2;
lst2.next.next = new ListNode();
lst2.next.next.value = 1;
ListNode.printSum(lst1, lst2);
}
}
Some considerations:
- Please format and indent better your code, it really helps you to read it better
- You create and assign x and y on every iteration. This let you assume that the initial value for them is always 0 on each iteration, so no need for the
elsestatements. - You are computing sum before compute x and y, so they will always be zero (I moved that instruction some lines down)
- In the
printSum()iteration you are never advancing from the initialresultelement, so the cycle will never end (I fixed that).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Roberto Mozzicato |
