LinkedList

                Never    
Java
       
public class LinkedList{
    public Node head;
    
    
    /* First Constructor:
     * Creates a linked list using the values from the given array. head will refer
     * to the Node that contains the element from a[0]
     */ 
    public LinkedList(Object [] a){
        head = new Node(a[0], null);
        Node slider = head;
        for(int i = 1; i < a.length; i++) {
            slider.next = new Node(a[i], null);
            slider = slider.next;
        }
    }
    
    /* Second Constructor:
     * Sets the value of head. head will refer
     * to the given LinkedList
     */
    public LinkedList(Node h){
        head = h;
    }
    
    /* Counts the number of Nodes in the list */
    public int countNode(){
        int counter = 0;
        
        Node slider = head;
        
        while(slider != null) {
            counter++;
            slider = slider.next;
        }
        
        return counter; // please remove this line!
    }
    
    /* prints the elements in the list */
    public void printList(){
        Node slider = head;
        
        while(slider != null) {
            System.out.print(slider.element + " ");
            slider = slider.next;
        }
        
        System.out.println();
    }
    
    // returns the reference of the Node at the given index. For invalid index return null.
    public Node nodeAt(int idx){
        if (idx < 0) return null;
        
        Node slider = head;
        
        for (int i = 0; slider != null; slider = slider.next, i++) {
            if (i == idx) return slider;
            if (i > idx) return null;
        }
        return null; // please remove this line!
    }
    
    
// returns the element of the Node at the given index. For invalid idx return null.
    public Object get(int idx){
        Node node = nodeAt(idx);
        
        if(node == null) return null;
        
        return node.element; // please remove this line!
    }
    
    
    
    /* updates the element of the Node at the given index. 
     * Returns the old element that was replaced. For invalid index return null.
     * parameter: index, new element
     */
    public Object set(int idx, Object elem){
        Node node = nodeAt(idx);
        
        if(node == null) return null;
        
        Object old = node.element;
        node.element = elem;
        
        return old; // please remove this line!
    }
    
    
    /* returns the index of the Node containing the given element.
     if the element does not exist in the List, return -1.
     */
    public int indexOf(Object elem){
        Node slider = head;
        
        for(int i = 0; slider != null; slider = slider.next, i++) {
            if(slider.element == elem) return i;
        }
        
        return -1; // please remove this line!
    }
    
    // returns true if the element exists in the List, return false otherwise.
    public boolean contains(Object elem){
        if (indexOf(elem) < 0) return false;
        
        return true; // please remove this line!
    }
    
    // Makes a duplicate copy of the given List. Returns the reference of the duplicate list.
    public Node copyList(){
        Node newList = new Node(head.element, null);
        Node sliderTarget = newList;
        Node sliderSource = head.next;
        
        while(sliderSource != null) {
            sliderTarget.next = new Node(sliderSource.element, null);
            sliderTarget = sliderTarget.next;
            sliderSource = sliderSource.next;
        }
        
        return newList; // please remove this line!
    }
    
    // Makes a reversed copy of the given List. Returns the head reference of the reversed list.
    public Node reverseList(){
        Node newList = new Node(head.element, null);
        Node sliderSource = head.next;
        
        while(sliderSource != null) {
            newList = new Node(sliderSource.element, newList);
            sliderSource = sliderSource.next;
        }
        
        return newList; // please remove this line!
    }
    
    /* inserts Node containing the given element at the given index
     * Check validity of index.
     */
    public void insert(Object elem, int idx){
        // TO DO
    }
    
    
    /* removes Node at the given index. returns element of the removed node.
     * Check validity of index. return null if index is invalid.
     */
    public Object remove(int index){
        // TO DO
        return null; // please remove this line!
    }
    
    // Rotates the list to the left by 1 position.
    public void rotateLeft(){
        // TO DO
    }
    
    // Rotates the list to the right by 1 position.
    public void rotateRight(){
        // TO DO
    }
    
}

Raw Text