Linked List in Java

Understanding Linked List in Java

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

Linked List

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-:

  1. Node – This part stores the data.
  2. 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

Linked List 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; } } }

Types of Linked Lists

There are many variations but below are the most important ones –

Singly Linked List

It is the most basic linked list, which is made up of several nodes, which can further be divided into two parts

  • data
  • next

Each node is connected to one another as the next value for each node holds the address to the next node in the sequence.

The first node is called as head and the last node is called as tail, where the last node’s next value is null.

Linked List Singly

Doubly Linked List

This is a successor of a normal linked list. Nodes of a double linked list have three parts – 

  • Data – Data / Value held
  • Next – Reference Address to the previous node
  • Next – Reference Address to next node

The main advantage is we can traverse in any direction forwards and backwards.

Doubly Linked List

Circular Linked List

It is very similar to Singly Linked List with one change that rather than the last node pointing to null. It has the address to the first (head) node.

Which makes it circular in Nature, it is used in OS to implement round robin scheduling algorithms too.

Insertion and Deletion in Linked List in Java

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

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription