Search code examples
javalinked-listsublist

Create a sublist of a linked list without using standard library


I'm trying to figure out how to create a sublist of a linked list without using the standard library for a practice exercise.

I have a solution coded but I'm not sure if this is working properly. I don't have any compile errors that are coming up but wanted a second opinion if there is a better way to do this or if corrections should be made.

LinkedList class basic instance variables

public class LinkedList<E> implements DynamicList<E> {

    LLNode<E> head;
    LLNode<E> tail;
    int llSize;



    LinkedList(){
        this.head = null;
        this.tail = null;
        this.llSize =0;
    } 

get method addressing LinkedList index

@Override
    public E get(int index) {
        LLNode<E> current = this.head;
        while(current.nextPointer != null){
            if(index == current.getIndex()){
                return current.getObj();
            }else{
                current = current.nextPointer;
            }
        }
        return null;
    }

Node class

public class LLNode<E>{
    E obj;
    LLNode<E> previousPointer;
    LLNode<E> nextPointer;
    int index;

    public LLNode(E obj){
        this.obj = obj;
        this.index=0;
    }

    public E getObj() {
        return obj;
    }

    public LLNode<E> getPreviousPointer() {
        return previousPointer;
    }

    public LLNode<E> getNextPointer() {
        return nextPointer;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}

Sublist method

@Override
    public DynamicList<E> subList(int start, int stop) {
        DynamicList<E> newDynamicList = new LinkedList<>();
        for(int i = start; i<stop; i++){
            newDynamicList.add(get(i));
        }


        return newDynamicList;
    }

Solution

  • As I'm seeing, that is a double linked list. As is suggested in comments, avoid using an index as part of the node itself, the index is part of the List, because the list controls the way each node is traversed to perform any operation (add, remove, find, etc)

    My suggestion (for sublist):

    • Check if the sublist is within the size of your list (you can throw some exception or return some default data, it depends on your design)
    • Move the index control to the list
    • For getting the sublist, you might have something like get the start node of the sublist, and then, use the nextPointer to traverse through the next nodes. You can calculate the size of the sublist and use that to control when you have to stop
    public DynamicList<E> subList(int start, int stop) {
            DynamicList<E> newDynamicList = new LinkedList<>();
    
            //here, you can validate the subList conditions to work (size, boundaries, etc)
            //an exception may be thrown if parameters do not meet some criteria
    
            int subListSize = stop - start;
    
            LLNode<E> current = get(start);
    
            while(newDynamicList.size() < subListSize){
                //Consider cloning the node and add it to the sublist
                newDynamicList.add(current);
                current = current.nextPointer;
            }
    
            return newDynamicList;
        }
    

    The main reason for not using the get method for retrieve each node is that get operation search for the node from the beginning each time you call it. It is better to get the start node and start traversing the nodes from there.

    Don't forget that created Sublist will contain a reference to the original list nodes. I suggest to clone the elements for avoiding affect the original node