A linked list in Java is a linear data structure that we can use for storing a large amount of data with ease.
The data stored in Linked List is not in a contiguous manner, but each data is stored at a different location, which can be accessed according to one’s need
Pro TipLinked List is a preferred data structure over arrays, as inserting and deleting data in a linked list is easier than in an array.
How to make a Linked List in Java
A linked list is a linear data structure that is made up of several nodes, which is further divided into two parts-:
Node – This part stores the data.
Link – This part stores the address of the memory location, where the next data of the list is stored.
Lets now have a look at Linked List implementation in Java
class LinkedList {
Node head; // head
// Linked list Node
class Node {
int data;
Node next;
// constructor to initialize
Node(int d) {
data = d; next = null;
}
}
}
Runimport java.lang.*;
class LinkedList {
Node head;
// not using parameterized constructor would by default
// force head instance to become null
// Node head = null; // can also do this, but not required
// Node Class
class Node{
int data;
Node next;
Node(int x) // parameterized constructor
{
data = x;
next = null;
}
}
public Node insert(int data)
{
// Creating newNode memory & assigning data value
Node newNode = new Node(data);
// assigning this newNode's next as current head node
newNode.next = head;
// re-assigning head to this newNode
head = newNode;
return head;
}
public void display()
{
Node node = head;
//as linked list will end when Node reaches Null
while(node!=null)
{
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
public void delete()
{
if (head == null){
System.out.println("List is empty, not possible to delete");
return;
}
System.out.println("Deleted: " + head.data);
// move head to next node
head = head.next;
}
}
class Main{
public static void main(String args[])
{
LinkedList ll = new LinkedList();
ll.insert(6);
ll.insert(5);
ll.insert(3);
ll.insert(4);
ll.insert(2);
ll.insert(1);
ll.display();
ll.delete();
ll.delete();
ll.display();
}
}
Output
1 2 4 3 5 6
Deleted: 1
Deleted: 2
4 3 5 6
Method 2
Runimport java.lang.*;
// Node Class
class Node {
int data;
Node next;
Node(int x) // parameterized constructor
{
data = x;
next = null;
}
}
class Main
{
static Node insertStart(Node head, int data)
{
// Creating newNode memory & assigning data value
Node newNode = new Node(data);
// assigning this newNode's next as current head node
newNode.next = head;
// re-assigning head to this newNode
head = newNode;
return head;
}
public static Node delete(Node head)
{
if (head == null){
System.out.println("List is empty, not possible to delete");
return head;
}
System.out.println("Deleted: " + head.data);
// move head to next node
head = head.next;
return head;
}
static void display(Node node) {
//as linked list will end when Node is Null
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
System.out.println("");
}
public static void main(String args[])
{
Node head = null;
head = insertStart(head,6);
head = insertStart(head,5);
head = insertStart(head,4);
head = insertStart(head,3);
head = insertStart(head,2);
head = insertStart(head,1);
display(head);
head = delete(head);
head = delete(head);
display(head);
}
}
Output
1 2 3 4 5 6
Deleted: 1
Deleted: 2
3 4 5 6
Linked List as a part of Collection
There are various different frameworks in Java which we can use for storing and operating efficiently on our data. Such is a framework known as Collection framework in Java. In this framework there are various different classes and interfaces are present using which we can code Linked List in Java in a very easy manner as it has a pre-defined class LinkedList stored in it, which has functions like
add()
delete()
search()
reverse(), etc
which we can use for operating on a linked list.
Linked List VS Array
Linked List
Array
Operation like insertion and deletion are easier.
Operation like searching and traversing are easier.
Data is stored in a continuous manner.
Data is stored in a contiguous manner.
No need for pre-allocating the size of the list.
User need to define the size before inserting the data.
Memory loss is comparatively low.
Memory loss is comparatively high.
Some of the pre-defined function of LinkedList class
Method Name
Method Description
void addFirst(E e)
This method inserts an element at the beginning of the list
void addLast(E e)
This method inserts an element at the end of the list
void add(int index, E element)
This method inserts an element at the specific position of the list
void clear()
It is used to clear all the elements from the list
boolean contains(Object o)
This method returns true if the specific element is present in the list, otherwise return false
element()
This element let us view the first element of the list
getLast ()
This method gives us the last element of the list
lastIndexOf(Object o)
This method returns the index of the last occurrence of a specified element
size()
This method returns the size of the list
toArray()
This method converts the list into an array
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment