public class FinalReview { // For LinkedIntList programming, the method would be added to the LinkedIntList class // A good exercise to understand how this works is to draw out a picture and verifies // if it works by hand! public LinkedIntList removeFirstOf3() { // Common Bug: Mixing up LinkedIntList and ListNode LinkedIntList other = new LinkedIntList(); if (front != null) { // Common Bug: Forgetting empty case // Set up front of other list, keep reference to end of list so we can add there other.front = front; ListNode back1 = other.front; // Update our front to remove first value, current will be pointing to // second node in original group front = front.next; ListNode current = front; // Common Bug: Having a slightly wrong loop condition that doesn't check all // the nulls it needs while (current != null && current.next != null && current.next.next != null) { current = current.next; // Move this node to the end of the other list back1.next = current.next; back1 = back1.next; // Update current to be in correct next position current.next = current.next.next; current = current.next; } // Common Bug: Forgetting to set fields to null if necessary back1.next = null; } // Common Bug: Forgetting to return return other; } // For Binary Tree programming, the method would be added to the IntTree class // A good exercise to understand how this works is to draw out a picture and verifies // if it works by hand! public void completeToLevel(int target) { if (target < 1) { throw new IllegalArgumentException(); } overallRoot = complete(overallRoot, target, 1); } private IntTreeNode complete(IntTreeNode root, int target, int level) { if (level <= target) { // Commnon Bug: Mixing up logic and not realizing we don't want an else branch for recursion if (root == null) { root = new IntTreeNode(-1); } // Common Bug: Forgetting x=change(x) pattern if it's necessary root.left = complete(root.left, target, level + 1); root.right = complete(root.right, target, level + 1); } return root; } }