// this contains the solution for NodeExercisesSimple.java
public class NodeExercisesSimple {

    public static void problem1() {
        // start:
        //
        // +---+ +---+---+ +---+---+
        // a | --+--> | 8 | --+--> | 6 | / |
        // +---+ +---+---+ +---+---+
        //
        // +---+ +---+---+ +---+---+
        // b | --+--> | 7 | --+--> | 5 | / |
        // +---+ +---+---+ +---+---+

        Node a = new Node(8, null);
        a.next = new Node(6, null);

        Node b = new Node(7, null);
        b.next = new Node(5, null);

        // end:
        //
        // +---+ +---+---+ +---+---+ +---+---+
        // a + --+--> | 8 | --+--> | 6 | --+--> | 7 | / |
        // +---+ +---+---+ +---+---+ +---+---+
        //
        // +---+ +---+---+
        // b | --+--> | 5 | / |
        // +---+ +---+---+

        // one possible solution
        a.next.next = b;
        b = b.next;
        a.next.next.next = null;

        /*
         * another possible solution that uses a temp variable
         * 
         * Node temp = b;
         * b.next = null;
         * a.next.next = b;
         * b = temp;
         */

        printChain(a);
        printChain(b);
    }

    public static void problem2() {
        // start:
        //
        // +---+ +---+---+
        // a | --+--> | 2 | / |
        // +---+ +---+---+
        //
        // +---+ +---+---+ +---+---+
        // b | --+--> | 0 | --+--> | 4 | / |
        // +---+ +---+---+ +---+---+

        Node a = new Node(2, null);

        Node b = new Node(0, null);
        b.next = new Node(4, null);

        // end:
        //
        // +---+
        // a | / |
        // +---+
        //
        // +---+ +---+---+ +---+---+ +---+---+
        // b | --+--> | 0 | --+--> | 2 | --+--> | 4 | / |
        // +---+ +---+---+ +---+---+ +---+---+

        // one possible solution
        a.next = b.next;
        b.next = a;
        a = null;

        printChain(a);
        printChain(b);
    }

    public static void problem3() {
        // start:
        //
        // +---+ +---+---+ +---+---+ +---+---+
        // a | --+--> | 5 | --+--> | 3 | --+--> | 8 | / |
        // +---+ +---+---+ +---+---+ +---+---+

        Node a = new Node(5, null);
        a.next = new Node(3, null);
        a.next.next = new Node(8, null);

        // end:
        //
        // +---+ +---+---+ +---+---+ +---+---+
        // a | --+--> | 3 | --+--> | 8 | --+--> | 5 | / |
        // +---+ +---+---+ +---+---+ +---+---+

        Node five = a;
        Node three = a.next;
        Node eight = a.next.next;

        five.next = null;
        eight.next = five;
        a = three;

        /*
         * alternate witohut temps;
         * 
         * a.next.next.next = a;
         * a = a.next;
         * a.next.next.next = null;
         */

        printChain(a);
    }

    public static void problem4() {
        // start:
        //
        // +---+ +---+---+ +---+---+
        // a | --+--> | 2 | --+--> | 6 | / |
        // +---+ +---+---+ +---+---+
        //
        // +---+ +---+---+ +---+---+
        // b | --+--> | 0 | --+--> | 4 | / |
        // +---+ +---+---+ +---+---+

        Node a = new Node(2, new Node(6, null));
        Node b = new Node(0, new Node(4, null));

        // end:
        //
        // +---+ +---+---+ +---+---+ +---+---+ +---+---+
        // a | --+--> | 0 | --+--> | 2 | --+--> | 4 | --+--> | 6 | / |
        // +---+ +---+---+ +---+---+ +---+---+ +---+---+
        //
        // +---+
        // b | / |
        // +---+
        //

        // using temporary variables
        Node zero = b;
        Node two = a;
        Node four = b.next;
        Node six = a.next;

        a = zero;
        zero.next = two;
        two.next = four;
        four.next = six;
        b = null;

        /*
         * without using temporary variables
         * 
         * b.next.next = a.next; // 4's next is now the node with 6
         * a.next = b.next; // 2's next is now the node with 4
         * b.next = a; // 0's next is now the node with 2
         * a = b;
         * b = null;
         */

        printChain(a);
        printChain(b);
    }

    public static void printChain(Node head) {

        System.out.println("---start----------------");
        /*
         * using a while loop
         * 
         * Node curr = head;
         * while (curr != null) {
         * System.out.println(curr.data);
         * curr = curr.next;
         * }
         */

        // using a for loop
        for (Node curr = head; curr != null; curr = curr.next) {
            System.out.println(curr.data);
        }
        System.out.println("---end------------------");
    }

    public static Node findFirstNegative(Node head) {
        return null;
    }

    public static void main(String[] args) {
        problem1();
        problem2();
        problem3();
        problem4();
    }
}
