Arrays are widely used in Java to store a fixed size collection of elements! However, they cannot be used to store dynamic or mixed (in term of data type) list of elements because they are static data structures and they are not dynamically resizable.

In this article, we will take a closer look at another data structure: Linked list which addresses some of the limitations raised by arrays.

We think that understanding how linked lists work internally will help you understanding more complex data structures! So, how to implement a linked list in java ? Read all the details to find out :)

What is a Linked List in Java ?

Without any doubt, Linked List is one of the most used data structure in computer programming. It is a dynamic data structure that describes a collection of data where all elements are linked together!

So basically, each element of the list is linked to the next one!

Confused? Let me explain: Like a chain, each element is linked to its neighbors. Since a linkedlist is not a loop, it must end somewhere! So, just after the last element, we find null reference to indicate the end of the list.

Linked list data structure

Each element of a linked list is called a node, it holds two items - the actual data and a reference to the next node! The head defines the entry point into the linked list!

Linked List Implementation in Java

As I mentioned earlier, a linked list is a dynamic data structure where all the elements are organized linearly! Linked lists and arrays are similar since they both can be used to store linear data!

However, unlike arrays, linked list elements are not stored in sequential memory locations, they are all linked in a linear way!

How to Implement a Linked List in Java?

There are several ways to represent a linkedlist in Java. The most basic idea is to use node chaining concept:

  • Each node contains an element of the list!

  • Each node contains data (to keep things simple, we will use an int value).

  • Each node holds a reference to the next element, except the last one in the list (which contains a null reference).

  • The list gives access to the first node, the rest of elements are accessible by moving from one node to another, according to their sequence.

In Java, an object is represented by a reference to the memory area where its variables and methods are stored.

Since a linked list is defined by a reference to a node (the first one in the list), then the simplest solution to implement linked list in Java is by using the node class!

    
        public class MylinkedlistNode {
            private int value;
            private MylinkedlistNode next;
            public MylinkedlistNode(int value, MylinkedlistNode next) { 
                this.value = value;
                this.next = next;
            }
        }
    

However, this representation works only for non-empty lists (MylinkedlistNode must contain at least one value).

In Java, Linked lists are usually implemented using two classes: a class for the nodes, and a class for the list itself, which contains a reference of Node class type.

Node Structure

Defining the node is the starting point to implement a linked list in java! You can imagine a node as a simple box that contain the actual data (int value in our example) and a link to another node! After all, a linked list is nothing but a group of nodes!

From technical point of view, you can use nested class concept to create your node inside your list class!

Java allows to define a class - inner class - within another class - outer class - , it is is a good practice to logically group classes that are only used in one place for the same purpose!

    
        // Linked list implementation 
        public class MylinkedlistImp {
            private Node node;
            // Node class 
            class Node { 
                private int value;  // data
                private Node next;  // link to next node
                public Node(int value) { 
                    this.value = value;
                    this.next = null; 
                }
                public Node(int value, Node next) { 
                    this.value = value;
                    this.next = next; 
                }
            }
        }
    

Conclusion:

That’s all folks! You have learned how to implement a linked list in Java and explored some of the most common operations. I hope this article will help you in getting started with LinkedList in Java programming.

Thank you for reading. Please drop me a comment on what you think about this topic or if you like to add something.