use of CtCILibrary.LinkedListNode in project ctci by careercup.
the class QuestionB method addListsHelper.
private static PartialSum addListsHelper(LinkedListNode l1, LinkedListNode l2) {
if (l1 == null && l2 == null) {
PartialSum sum = new PartialSum();
return sum;
}
PartialSum sum = addListsHelper(l1.next, l2.next);
int val = sum.carry + l1.data + l2.data;
LinkedListNode full_result = insertBefore(sum.sum, val % 10);
sum.sum = full_result;
sum.carry = val / 10;
return sum;
}
use of CtCILibrary.LinkedListNode in project ctci by careercup.
the class QuestionB method addLists.
private static LinkedListNode addLists(LinkedListNode l1, LinkedListNode l2) {
int len1 = length(l1);
int len2 = length(l2);
if (len1 < len2) {
l1 = padList(l1, len2 - len1);
} else {
l2 = padList(l2, len1 - len2);
}
PartialSum sum = addListsHelper(l1, l2);
if (sum.carry == 0) {
return sum.sum;
} else {
LinkedListNode result = insertBefore(sum.sum, sum.carry);
return result;
}
}
use of CtCILibrary.LinkedListNode in project ctci by careercup.
the class QuestionB method padList.
private static LinkedListNode padList(LinkedListNode l, int padding) {
LinkedListNode head = l;
for (int i = 0; i < padding; i++) {
LinkedListNode n = new LinkedListNode(0, null, null);
head.prev = n;
n.next = head;
head = n;
}
return head;
}
use of CtCILibrary.LinkedListNode in project ctci by careercup.
the class QuestionA method main.
public static void main(String[] args) {
LinkedListNode lA1 = new LinkedListNode(9, null, null);
LinkedListNode lA2 = new LinkedListNode(9, null, lA1);
LinkedListNode lA3 = new LinkedListNode(9, null, lA2);
LinkedListNode lB1 = new LinkedListNode(1, null, null);
LinkedListNode lB2 = new LinkedListNode(0, null, lB1);
LinkedListNode lB3 = new LinkedListNode(0, null, lB2);
LinkedListNode list3 = addLists(lA1, lB1, 0);
System.out.println(" " + lA1.printForward());
System.out.println("+ " + lB1.printForward());
System.out.println("= " + list3.printForward());
int l1 = linkedListToInt(lA1);
int l2 = linkedListToInt(lB1);
int l3 = linkedListToInt(list3);
System.out.print(l1 + " + " + l2 + " = " + l3 + "\n");
System.out.print(l1 + " + " + l2 + " = " + (l1 + l2));
}
use of CtCILibrary.LinkedListNode in project ctci by careercup.
the class QuestionB method insertBefore.
private static LinkedListNode insertBefore(LinkedListNode list, int data) {
LinkedListNode node = new LinkedListNode(data, null, null);
if (list != null) {
list.prev = node;
node.next = list;
}
return node;
}
Aggregations