top of page

Java LinkedList

A Linked List is a sequence of data structures, which are connected together via links.

Linked List is a sequence of links which contains items. Each link contains a connection to another link. Linked list is the second most-used data structure after array.

Linked List is a part of the Collection framework present in java.util package. The LinkedList class of the Java collections framework provides the functionality of the linked list data structure (doubly linkedlist).




Each element in a linked list is known as a node. It consists of 3 fields:

  • Previous - stores an address of the previous element in the list. It is null for the first element

  • Next - stores an address of the next element in the list. It is null for the last element

  • Data - stores the actual data


Why do we need a Linked List?


You must be aware of the arrays which is also a linear data structure but arrays have certain limitations such as:

1) Size of the array is fixed which is decided when we create an array so it is hard to predict the number of elements in advance, if the declared size fall short then we cannot increase the size of an array and if we declare a large size array and do not need to store that many elements then it is a waste of memory.


2) Array elements need contiguous memory locations to store their values.


3) Inserting an element in an array is performance wise expensive as we have to shift several elements to make a space for the new element. For example: Let’s say we have an array that has following elements: 10, 12, 15, 20, 4, 5, 100, now if we want to insert a new element 99 after the element that has value 12 then we have to shift all the elements after 12 to their right to make space for new element.

Similarly deleting an element from the array is also a performance wise expensive operation because all the elements after the deleted element have to be shifted left.

  • These limitations are handled in the Linked List by providing following features: 1. Linked list allows dynamic memory allocation, which means memory allocation is done at the run time by the compiler and we do not need to mention the size of the list during linked list declaration.

  • Linked list elements don’t need contiguous memory locations because elements are linked with each other using the reference part of the node that contains the address of the next node of the list.

  • Insert and delete operations in the Linked list are not performance wise expensive because adding and deleting an element from the linked list does’t require element shifting, only the pointer of the previous and the next node requires change.


Hierarchy of LinkedList class in Java









Implementation of LinkedList in Java -


Here is how we can create linked lists in Java (Syntax) :

LinkedList<Type> linkedList = new LinkedList<>();

Here, Type indicates the type of a linked list. For example,

// create String type linked list
LinkedList<String> linkedList = new LinkedList<>();

Example: Create LinkedList in Java

import java.util.LinkedList;

class Main {
  public static void main(String[] args){

    // create linkedlist
    LinkedList<String> char = new LinkedList<>();

    // Add elements to LinkedList
    char.add("A");
    char.add("B");
    char.add("C");
    System.out.println("LinkedList: " + char);
  }
}

Output-


LinkedList: [A, B, C]

Are you looking for help in Java task like project, assignment and Want to chat about your team? We provide team extension services for early-stage startups. Please check out our services page and send a help request at contact@codersarts.com

 
If you are looking for any kind of Help in Web development assignment help Contact us.
bottom of page