How to create our own linked list in Java

Each node is an object of class. Node can store an int, and a reference to next Node object. Also note that you can use a complex object, as a member instead of an int.

As shown in the example, next reference of last element is null.

while loop is used to iterate thru the elements in the linked list.
Also, more methods can be added which performs adding an element, deleting an element, etc….

Below is a Program of singly linked list in Java.

class Node
{
	private int i;
	
	Node next;

	Node(int i)
	{
		this.i = i;
	}
	public void set(int i)
	{
		this.i = i;
	}
	
	public int get()
	{
		return i;
	}
	
}

public class OwnLinkedList {
	public static void main(String args[])
	{
	//create elements, and assign next element
	Node n1 = new Node(10);
		
	Node n2 = new Node(20);
	n1.next = n2;
		
	Node n3 = new Node(30);
	n2.next = n3;
		
	Node n4 = new Node(40);
	n3.next = n4;
		
	Node n5 = new Node(50);
	n4.next = n5;
	n5.next = null;
		
	Node tmp = n1;
	while(tmp.next!=null)
	{
		System.out.println(tmp.get());
		tmp = tmp.next;
	}
		
	}
}

double linked list
double linked list

Below is program on how to implement double linked list. Advantage of doubly linked list is it can be traversed in both the directions, unlike singly linked list. As shown below, each Node1 has reference to previous element, along with reference to next element. previous reference for first element is null. next reference for last element is null.

class Node1
{
	private int i;
	
	Node1 next;
	Node1 previous;

	Node1(int i)
	{
		this.i = i;
	}
	public void set(int i)
	{
		this.i = i;
	}
	
	public int get()
	{
		return i;
	}
	
}

public class OwnDLinkedList {
	public static void main(String args[])
	{
		//create elements, and assign next and previous element
		Node1 n1 = new Node1(10);
		
		n1.previous = null;
		Node1 n2 = new Node1(20);
		n1.next = n2;
		n2.previous = n1;
		
		Node1 n3 = new Node1(30);
		n2.next = n3;
		n3.previous = n2;
		
		Node1 n4 = new Node1(40);
		n3.next = n4;
		n4.previous = n3;
		
		Node1 n5 = new Node1(50);
		n4.next = n5;
		n5.previous = n4;
		n5.next = null;
		
		Node1 tmp = n1;
		while(tmp.next!=null)
		{
			System.out.println(tmp.get());
			tmp = tmp.next;
		}
		
	}
}

What is circular linked list

Below is an implementation of Circular doubly linked list. Here Head node refers to starting node, Tail node refers to last node.

Circular Linked List
Circular Linked List

public class OwnCLinkedList {
	public static void main(String args[])
	{
		Node1 n_head, n_tail;
		//create elements, and assign next and previous element
		Node1 n1 = new Node1(10);
		
		Node1 n5 = new Node1(50);
		
		n_head = n1; //set head node
		n_tail = n5; //set tail node
		
		n1.previous = n5;
		Node1 n2 = new Node1(20);
		n1.next = n2;
		n2.previous = n1;
		
		Node1 n3 = new Node1(30);
		n2.next = n3;
		n3.previous = n2;
		
		Node1 n4 = new Node1(40);
		n3.next = n4;
		n4.previous = n3;
		
		
		n4.next = n5;
		n5.previous = n4;
		n5.next = n1;
		
		Node1 tmp = n_head;
		while(tmp!=n_tail)
		{
			System.out.println(tmp.get());
			tmp = tmp.next;
		}
		
	}
}

We can make above Data structures better useful by providing methods, to add and delete Node,etc…

Similar steps can be followed to build complex data structures such as Graph

You may also like to read:
How Vector and ArrayList differ
List and Set differences
How ArrayList and LinkedList are implemented internally

Difference between a Reference and an object, in Java

class Tuv{
	int a, b;
}

public class ReferenceDemo {
	public static void main(String args[])
	{
	Tuv obj/*name of reference*/ = new Tuv()/*creating object*/;
	//obj is the reference to Tuv object
		
	Tuv a_obj = obj;
	//creating another reference, and assigning to an existing object
		
	Tuv b_obj;//created reference, and not the object
		
	new Tuv().a = 50;//anonymous object, can be used only once
	//anonymous object does not have reference
		
	obj.a = 10;
	obj.b = 20;
		
	System.out.println("Before invoking check():"+obj.a+" "+obj.b);
		
	check(obj);
		
	System.out.println("After invoking check():"+obj.a+" "+obj.b);
	}
	
	static void check(Tuv tmp)
	{
	System.out.println("in check(), b4 changing:"+tmp.a+" "+tmp.b);
		
	tmp.a = 40;
	tmp.b = 50;
		
	System.out.println("in check(), after changing:"+tmp.a+" "+tmp.b);
	}
}