Write code to sum two numbers represented by a linked list. The digits in this linked list are in reverse order. eg. (9->2->3) + (4->8->2) = (3->1->6)
Any comments on my solution (especially on the testing part)?
public class ListNode {
private int val;
ListNode next;
ListNode(int x) {
val = x;
}
ListNode(ListNode other){
val = other.val;
}
boolean hasNext() {
if (this.next != null) {
return true;
} else {
return false;
}
}
public int getVal(){
return this.val;
}
public void setVal(int v){
this.val = v;
}
}
public class SumTwo {
/**
* Iterative
* @param l1
* @param l2
* @return
*/
public ListNode addTwoNumbersV1(ListNode l1, ListNode l2) {
if (l1 == null){
l1 = new ListNode(0);
}
if (l2 == null){
l2 = new ListNode(0);
}
int carry = 0;
int total = l1.getVal() + l2.getVal() + carry;
int num = total % 10;
carry = total / 10;
ListNode result = new ListNode(num);
ListNode r = result;
l1 = l1.next;
l2 = l2.next;
while (l1 != null || l2 != null) {
if (l1 == null){
l1 = new ListNode(0);
}
if (l2 == null){
l2 = new ListNode(0);
}
total = l1.getVal() + l2.getVal() + carry;
num = total % 10;
carry = total / 10;
r.next = new ListNode(num);
r = r.next;
l1 = l1.next;
l2 = l2.next;
}
// if carry != 0 then add it
if (carry == 1){
r.next = new ListNode(1);
}
return result;
}
public class SumTwoUtils {
public String printLinkedList(ListNode l){
StringBuffer s = new StringBuffer();
while (l != null){
s.append(l.getVal());
if (l.next != null){
s.append("->");
}
l = l.next;
}
return s.toString();
}
}
public class TestSumTwo {
@Test
public void testPrintLinkedList(){
ListNode l = new ListNode(1);
ListNode l2 = l;
l2.next = new ListNode(2);
l2 = l2.next;
l2.next = new ListNode(3);
String expected = "1->2->3";
SumTwoUtils ut = new SumTwoUtils();
String result = ut.printLinkedList(l);
assertEquals(expected, result);
}
@Test
public void testSameLinkedListNoCarry(){
// Create list 1->2->3
ListNode l = new ListNode(1);
ListNode l2 = l;
l2.next = new ListNode(2);
l2 = l2.next;
l2.next = new ListNode(3);
// sum it to itself
SumTwo sm = new SumTwo();
SumTwoUtils ut = new SumTwoUtils();
ListNode n = sm.addTwoNumbersV1(l,l);
String result = ut.printLinkedList(n);
String expected = "2->4->6";
assertEquals(expected, result);
}
@Test
public void testSameSizeNoCarry(){
// Create list 1->2->3
ListNode l1 = new ListNode(1);
ListNode ll1 = l1;
ll1.next = new ListNode(2);
ll1 = ll1.next;
ll1.next = new ListNode(3);
// Create list 4->6->2
ListNode l2 = new ListNode(4);
ListNode ll2 = l2;
ll2.next = new ListNode(6);
ll2 = l2.next;
ll2.next = new ListNode(2);
// sum them
SumTwo sm = new SumTwo();
SumTwoUtils ut = new SumTwoUtils();
ListNode n = sm.addTwoNumbersV1(l1,l2);
// Expected result
String expected = "5->8->5";
// Result
String result = ut.printLinkedList(n);
assertEquals(expected, result);
}
@Test
public void testSameSizeAndCarry(){
// Create list 9->2->3
ListNode l1 = new ListNode(9);
ListNode ll1 = l1;
ll1.next = new ListNode(2);
ll1 = ll1.next;
ll1.next = new ListNode(3);
// Create list 4->8->2
ListNode l2 = new ListNode(4);
ListNode ll2 = l2;
ll2.next = new ListNode(8);
ll2 = l2.next;
ll2.next = new ListNode(2);
// sum them
SumTwo sm = new SumTwo();
SumTwoUtils ut = new SumTwoUtils();
ListNode n = sm.addTwoNumbersV1(l1,l2);
// Expected result
String expected = "3->1->6";
// Result
String result = ut.printLinkedList(n);
assertEquals(expected, result);
}
@Test
public void testSameSizeCarryEnd(){
// Create list 1->2->8
ListNode l1 = new ListNode(1);
ListNode ll1 = l1;
ll1.next = new ListNode(2);
ll1 = ll1.next;
ll1.next = new ListNode(8);
// Create list 4->7->8
ListNode l2 = new ListNode(4);
ListNode ll2 = l2;
ll2.next = new ListNode(7);
ll2 = l2.next;
ll2.next = new ListNode(8);
// sum them
SumTwo sm = new SumTwo();
SumTwoUtils ut = new SumTwoUtils();
ListNode n = sm.addTwoNumbersV1(l1,l2);
// Expected result
String expected = "5->9->6->1";
// Result
String result = ut.printLinkedList(n);
assertEquals(expected, result);
}
@Test
public void testDifferentSizesNoCarry(){
// Create list 2->8
ListNode l1 = new ListNode(2);
ListNode ll1 = l1;
ll1.next = new ListNode(8);
// Create list 4->1->8
ListNode l2 = new ListNode(4);
ListNode ll2 = l2;
ll2.next = new ListNode(1);
ll2 = l2.next;
ll2.next = new ListNode(8);
// sum them
SumTwo sm = new SumTwo();
SumTwoUtils ut = new SumTwoUtils();
ListNode n = sm.addTwoNumbersV1(l1,l2);
// Expected result
String expected = "6->9->8";
// Result
String result = ut.printLinkedList(n);
assertEquals(expected, result);
}
@Test
public void testDifferentSizesAndCarry(){
// Create list 2->8
ListNode l1 = new ListNode(2);
ListNode ll1 = l1;
ll1.next = new ListNode(8);
// Create list 4->7->8
ListNode l2 = new ListNode(4);
ListNode ll2 = l2;
ll2.next = new ListNode(7);
ll2 = l2.next;
ll2.next = new ListNode(8);
// sum them
SumTwo sm = new SumTwo();
SumTwoUtils ut = new SumTwoUtils();
ListNode n = sm.addTwoNumbersV1(l1,l2);
// Expected result
String expected = "6->5->9";
// Result
String result = ut.printLinkedList(n);
assertEquals(expected, result);
}
java.util.LinkedList? \$\endgroup\$