IC312: Data Structures (FA17)


Home Policy Calendar Units Assignments Resources

HW 3: Recursive Linked List [SOLUTION]

Solution

private Node max(Node n, Node curMax){
    if(n == null) return curMax;

    int cmp = n.getData().compareTo(curMax.getData());
    if(cmp >= 0){
        return max(n.getNext(),n);
    }else{
        return max(n.getNext(),curMax);
    }

}


private Node remove(Node n, T e){
    if(n==null){
        return null;
    }

    if(e.equals(n.getData())){
        return remove(n.getNext(),e);
    }

    n.setNext(remove(n.getNext(),e));
    return n;

}

private void duplicate(Node n){
    if(n == null){
        return;
    }

    Node newNode = new Node(n.getData());
    newNode.setNext(n.getNext());
    n.setNext(newNode);

    duplicate(newNode.getNext());
}

private String toStringReverse(Node n){
    if( n == null) return "";
    return toStringReverse(n.getNext())+" "+n.getData().toString();
}