Table of Contents
In this post, we will see how to implement singly linked list in java.
It is one of the most used data structure. In singly linked list, Node has data and pointer to next node. It does not have pointer to the previous node. Last node ‘s next points to null, so you can iterate over linked list by using this condition.
Node for linked list can be presented as below:
Lets create Main class named LinkedListMain.java to create LinkedList.
When you run above program, you will get below output:
It is one of the most used data structure. In singly linked list, Node has data and pointer to next node. It does not have pointer to the previous node. Last node ‘s next points to null, so you can iterate over linked list by using this condition.
Node for linked list can be presented as below:
1 2 3 4 5 6 7 8 9 10 |
class Node { public int data; public Node next; public void displayNodeData() { System.out.println("{ " + data + " } "); } } |
An example of linked list:
Let’s implement Linked List in java.
Create a java file named SinglyLinkedList.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
package org.arpit.java2blog; class Node { public int data; public Node next; public void displayNodeData() { System.out.println("{ " + data + " } "); } } public class SinglyLinkedList { private Node head; public boolean isEmpty() { return (head == null); } // used to insert a node at the start of linked list public void insertFirst(int data) { Node newNode = new Node(); newNode.data = data; newNode.next = head; head = newNode; } // used to delete node from start of linked list public Node deleteFirst() { Node temp = head; head = head.next; return temp; } // Use to delete node after particular node public void deleteAfter(Node after) { Node temp = head; while (temp.next != null && temp.data != after.data) { temp = temp.next; } if (temp.next != null) temp.next = temp.next.next; } // used to insert a node at the start of linked list public void insertLast(int data) { Node current = head; while (current.next != null) { current = current.next; // we'll loop until current.next is null } Node newNode = new Node(); newNode.data = data; current.next = newNode; } // For printing Linked List public void printLinkedList() { System.out.println("Printing LinkedList (head --> last) "); Node current = head; while (current != null) { current.displayNodeData(); current = current.next; } System.out.println(); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package org.arpit.java2blog; public class LinkedListMain { public static void main(String args[]) { SinglyLinkedList myLinkedlist = new SinglyLinkedList(); myLinkedlist.insertFirst(5); myLinkedlist.insertFirst(6); myLinkedlist.insertFirst(7); myLinkedlist.insertFirst(1); myLinkedlist.insertLast(2); // Linked list will be // 2 -> 1 -> 7 -> 6 -> 5 Node node=new Node(); node.data=1; myLinkedlist.deleteAfter(node); // After deleting node after 1,Linked list will be // 2 -> 1 -> 6 -> 5 myLinkedlist.printLinkedList(); } } |
1 2 3 4 5 6 7 |
Printing LinkedList (head --> last) { 1 } { 6 } { 5 } { 2 } |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.
Nice Sir Arpit ~! 🙂
Beautifully written sir. Thank you 🙂
Hi
good job.
Your comment before insertLast(int data) method is wrong.
This was the best implementation of Linked List in java I’ve seen on the internet. Thank you so much, cleared all my concepts.
This was the way I was looking up for.
Thank you so much.