Linked list operations for a competition After checking some incoming coding competitions, I feel I may want to develop a little bit more on my coding skills and style using some basic algorithms/data structures.

The following two pieces of Java code are about some LinkedList operation. Can you take a quick look and give me some feedback about the accuracy, efficiency, better solution, or code style?

The Java code includes some simple comments, test cases, and it is tested at Java 6 and reviewed by myself.

/*
* purpose of program - for a singly linkedList, switch the elements at odd number positions with
* the ones at even number positions
*
input sample : 1&gt;2&gt;3&gt;4&gt;5; expecting output: 2&gt;1&gt;4&gt;3&gt;5;
input sample : 1&gt;2&gt;3&gt;4; expecting output: 2&gt;1&gt;4&gt;3;

*/




ListTwister.java

package com.algo;


/*
 * purpose of program - for a singly linkedList, switch the elements at odd number positions with 
 * the ones at even number positions 
 * 
input sample : 1&gt;2&gt;3&gt;4&gt;5; expecting output: 2&gt;1&gt;4&gt;3&gt;5;
input sample : 1&gt;2&gt;3&gt;4;   expecting output: 2&gt;1&gt;4&gt;3;

 */

public class ListTwister {

    /* for odd sequence element (array index = 0, 2, etc), store a "branch head", store prev node, and move to next;
    for even sequence element (array index = 1, 3, etc), store the "nextNode", twist current and prev node, link the "branch head" to the currentNode, and link the "prev node" to cached "nextNode"
     */
    public static Node twist(Node head) {
        Node current = head, newHead = null, branchHead = null, tempNext = null, prev = null ;
        int index = 0;

        if (head!=null &amp; head.next!=null ) {
            newHead = head.next;
        }
        else {
            newHead = head;
        }

        while (current != null) {
            //odd sequence 
            if (index % 2 == 0) {
                branchHead  = prev;
                prev = current;
                current = current.next;
            }           
            //even sequence 
            else {
                tempNext = current.next;
                current.next = prev;
                if (branchHead != null ) {
                    branchHead.next = current;
                }
                prev.next = tempNext;
                current = tempNext;             
            }

            index ++;
        } // end of while

        return newHead;

    } // end of twist()

    public static void main (String args[]) {
        Node head_list1 = new Node(1, null);
        Node node2_list1 = new Node(2, null);
        Node node3_list1 = new Node(3, null);
        Node node4_list1 = new Node(4, null);
        Node node5_list1 = new Node(5, null);

        head_list1.add(node2_list1 );
        node2_list1.add(node3_list1 );
        node3_list1.add(node4_list1 );
        node4_list1.add(node5_list1 );

        Node head_list2 = new Node(1, null);
        Node node2_list2 = new Node(2, null);
        Node node3_list2 = new Node(3, null);
        Node node4_list2 = new Node(4, null);

        head_list2.add(node2_list2 );
        node2_list2.add(node3_list2 );
        node3_list2.add(node4_list2 );

        head_list1.print();
        //expect to output : 2-&gt;1-&gt;4-&gt;3-&gt;5-&gt;END.        
        twist(head_list1).print();

        head_list2.print();
        //expect to output : 2-&gt;1-&gt;4-&gt;3-&gt;END.       
        twist(head_list2).print();      

    } // end of main()

} // end of class




Node.java

package com.algo;

/*
Node for singly LinkedList;
 */

public class Node {
    public int value;
    public Node next;

    public Node (int value, Node next) {
        this.value = value;
        this.next = next;
    }

    public void add(Node node) {
        this.next = node;
    }

    public void print() {
        Node current = this;
        while (current!=null ) {
            System.out.print(current.value + "-&gt;");
            current = current.next;
        }
        if (current==null) {
            System.out.println("END.");
        } 
    }
} // end of class
Rename Node.add to setNext, since its a setter method:

public void setNext(final Node node) {
    this.next = node;
}




Create a constructor with only the value parameter:

public Node(final int value) {
    this.value = value;
}


and call this in your main method instead of passing nulls:

final Node head_list1 = new Node(1);




Do not use public fields:

public int value;
public Node next;


Make them private and provide getters and setters.



The value field could be final:

public class Node {
    private final int value;
    ...
}


It should not change, so protect it with final.



Instead of the main method write self-checking unit tests. The posted main should be separated to two test.



The Node class shouldn't be responsible for printing. It should return a String instead of System.out.print() calls. Maybe another client wants to write it to a file or a socket, but System.out.print() made it impossible.



Declare only one variable per line.



    while (current != null) {
        System.out.print(current.value + "-&gt;");
        current = current.next;
    }
    if (current == null) {
        System.out.println("END.");
    }


The current == null condition seems always true, so it's unnecessary:

    while (current != null) {
        System.out.print(current.value + "-&gt;");
        current = current.next;
    }
    System.out.println("END.");




Check input:

public static Node twist(final Node head) {
    if (head == null) {
        throw new NullPointerException("head cannot be null");
    }
    ...
}




It's unnecessary to set default value for all variables to null:

Node newHead = null;


Furthermore, it could be final since it get its value before it will be used:

final Node newHead;




Read Effective Java, Second Edition, Item 45: Minimize the scope of local variables. (Google for "minimize the scope of local variables", it's on Google Books too.)

For example, tempNext should be declared inside the else branch:

else {
    final Node tempNext = current.next;
    current.next = prev;
    if (branchHead != null) {
        branchHead.next = current;
    }
    prev.next = tempNext;
    current = tempNext;
}




Use camelCase variable names: head_list1 -> headList1. (Effective Java, Second Edition, Item 56: Adhere to generally accepted naming conventions)