Make linked list in Java

Make linked list in Java

If you’re actually building a real production system, then yes, you’d typically just use the stuff in the standard library if what you need is available there. That said, don’t think of this as a pointless exercise.

It’s good to understand how things work, and understanding linked lists is an important step towards understanding more complex data structures, many of which don’t exist in the standard libraries.

There are some differences between the way you’re creating a linked list and the way the Java collections API does it.

The Collections API is trying to adhere to a more complicated interface. Your LinkedList will always have at least one element.

With this kind of setup you’d use null for when you need an empty list. Think of “next” as being “the rest of the list”. In fact many people would call it tail instead of “next”.

Here’s a diagram of a singly LinkedList:

Make linked list in Java

Another must read:

What’s the best way to make a linkedlist in Java from scratch?

Well, here is a simplest implementation of LinkedList Class in Java.

Make linked list in Java

package crunchify.com.tutorial;

* last updated: 11/10/2015

public class CrunchifyLinkedListTest {

public static CrunchifyLinkedList crunchifyList;

public static void main(String[] args) {

// Default constructor - let's put "0" into head element.

crunchifyList = new CrunchifyLinkedList();

// add more elements to LinkedList

* Please note that primitive values can not be added into LinkedList directly. They must be converted to their

* corresponding wrapper class.

System.out.println("Print: crunchifyList: \t\t" + crunchifyList);

System.out.println(".size(): \t\t\t\t" + crunchifyList.size());

System.out.println(".get(3): \t\t\t\t" + crunchifyList.get(3) + " (get element at index:3 - list starts from 0)");

System.out.println(".remove(2): \t\t\t\t" + crunchifyList.remove(2) + " (element removed)");

System.out.println(".get(3): \t\t\t\t" + crunchifyList.get(3) + " (get element at index:3 - list starts from 0)");

System.out.println(".size(): \t\t\t\t" + crunchifyList.size());

System.out.println("Print again: crunchifyList: \t" + crunchifyList);

class CrunchifyLinkedList {

private static int counter;

public CrunchifyLinkedList() {

// appends the specified element to the end of this list.

public void add(Object data) {

// Initialize Node only incase of 1st element

Node crunchifyTemp = new Node(data);

Node crunchifyCurrent = head;

// Let's check for NPE before iterate over crunchifyCurrent

if (crunchifyCurrent != null) {

// starting at the head node, crawl to the end of the list and then add element after last node

while (crunchifyCurrent.getNext() != null) {

crunchifyCurrent = crunchifyCurrent.getNext();

// the last node's "next" reference set to our new node

crunchifyCurrent.setNext(crunchifyTemp);

// increment the number of elements variable

private static int getCounter() {

private static void incrementCounter() {

private void decrementCounter() {

// inserts the specified element at the specified position in this list

public void add(Object data, int index) {

Node crunchifyTemp = new Node(data);

Node crunchifyCurrent = head;

// Let's check for NPE before iterate over crunchifyCurrent

if (crunchifyCurrent != null) {

// crawl to the requested index or the last element in the list, whichever comes first

for (int i = 0; i < index && crunchifyCurrent.getNext() != null; i++) {

crunchifyCurrent = crunchifyCurrent.getNext();

// set the new node's next-node reference to this node's next-node reference

crunchifyTemp.setNext(crunchifyCurrent.getNext());

// now set this node's next-node reference to the new node

crunchifyCurrent.setNext(crunchifyTemp);

// increment the number of elements variable

public Object get(int index)

// returns the element at the specified position in this list.

// index must be 1 or higher

Node crunchifyCurrent = null;

crunchifyCurrent = head.getNext();

for (int i = 0; i < index; i++) {

if (crunchifyCurrent.getNext() == null)

crunchifyCurrent = crunchifyCurrent.getNext();

return crunchifyCurrent.getData();

// removes the element at the specified position in this list.

public boolean remove(int index) {

// if the index is out of range, exit

if (index < 1 || index > size())

Node crunchifyCurrent = head;

for (int i = 0; i < index; i++) {

if (crunchifyCurrent.getNext() == null)

crunchifyCurrent = crunchifyCurrent.getNext();

crunchifyCurrent.setNext(crunchifyCurrent.getNext().getNext());

// decrement the number of elements variable

// returns the number of elements in this list.

public String toString() {

Node crunchifyCurrent = head.getNext();

while (crunchifyCurrent != null) {

output += "[" + crunchifyCurrent.getData().toString() + "]";

crunchifyCurrent = crunchifyCurrent.getNext();

// reference to the next node in the chain, or null if there isn't one.

// data carried by this node. could be of any type you need.

public Node(Object dataValue) {

// another Node constructor if we want to specify the node to point to.

@SuppressWarnings("unused")

public Node(Object dataValue, Node nextValue) {

// these methods should be self-explanatory

public Object getData() {

@SuppressWarnings("unused")

public void setData(Object dataValue) {

public void setNext(Node nextValue) {

Few things:

Here we are initialize Node only while adding 1st element.

// Initialize Node only incase of 1st element

Result:

Print: crunchifyList: [1][2][3][4][5]

.get(3): 4 (get element at index:3 - list starts from 0)

.remove(2): true (element removed)

.get(3): 5 (get element at index:3 - list starts from 0)

Print again: crunchifyList: [1][2][4][5]

Enhancements to this implementation include making it a double-linked list, adding methods to insert and delete from the middle or end, and by adding get and sort methods as well.

Referenced answer from Stack Overflow by Laurence Gonsalves. You may be interested in list of all Java Tutorials.

If you liked this article, then please share it on social media. Still have any questions about an article, leave us a comment.