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:

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.

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

Chủ Đề