use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.
the class QuestionA method addLists.
private static LinkedListNode addLists(LinkedListNode l1, LinkedListNode l2, int carry) {
if (l1 == null && l2 == null && carry == 0) {
return null;
}
LinkedListNode result = new LinkedListNode();
int value = carry;
if (l1 != null) {
value += l1.data;
}
if (l2 != null) {
value += l2.data;
}
result.data = value % 10;
if (l1 != null || l2 != null) {
LinkedListNode more = addLists(l1 == null ? null : l1.next, l2 == null ? null : l2.next, value >= 10 ? 1 : 0);
result.setNext(more);
}
return result;
}
use of CtCILibrary.LinkedListNode in project CtCI-6th-Edition by careercup.
the class QuestionB method main.
public static void main(String[] args) {
LinkedListNode lA1 = new LinkedListNode(3, null, null);
LinkedListNode lA2 = new LinkedListNode(1, null, lA1);
LinkedListNode lB1 = new LinkedListNode(5, null, null);
LinkedListNode lB2 = new LinkedListNode(9, null, lB1);
LinkedListNode lB3 = new LinkedListNode(1, null, lB2);
LinkedListNode list3 = addLists(lA1, lB1);
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-6th-Edition 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-6th-Edition by careercup.
the class QuestionA method main.
public static void main(String[] args) {
int length = 9;
LinkedListNode[] nodes = new LinkedListNode[length];
for (int i = 0; i < length; i++) {
nodes[i] = new LinkedListNode(i >= length / 2 ? length - i - 1 : i, null, null);
}
for (int i = 0; i < length; i++) {
if (i < length - 1) {
nodes[i].setNext(nodes[i + 1]);
}
if (i > 0) {
nodes[i].setPrevious(nodes[i - 1]);
}
}
// nodes[length - 2].data = 9; // Uncomment to ruin palindrome
LinkedListNode head = nodes[0];
System.out.println(head.printForward());
System.out.println(isPalindrome(head));
}
use of CtCILibrary.LinkedListNode in project ctci by careercup.
the class Question method main.
public static void main(String[] args) {
//AssortedMethods.randomLinkedList(1000, 0, 2);
LinkedListNode first = new LinkedListNode(0, null, null);
LinkedListNode head = first;
LinkedListNode second = first;
for (int i = 1; i < 8; i++) {
second = new LinkedListNode(i % 2, null, null);
first.setNext(second);
second.setPrevious(first);
first = second;
}
System.out.println(head.printForward());
LinkedListNode clone = head.clone();
deleteDupsA(head);
System.out.println(head.printForward());
deleteDupsC(clone);
}
Aggregations