Back to all blogs

MadAlgos Blog

Introduction to LinkedList ep03

M
Mansha Srivastava18 May 2023
Introduction to LinkedList ep03

Implementation of Linked List in java

 

LinkedLists are a basic data structure used in various programming scenarios to store and manage data collections.

In this blog, we will explore the implementation of LinkedLists in Java, focusing on their structure and creation mainly.

Get ready to dive into the world of LinkedLists and unlock their potential for effectively organizing and manipulating data.

 

The following code shows the implementation of LinkedList:

 

class LL {

  // create an object of Node class
  // represent the head of the linked list
  Node head;

  // static inner class
  static class Node {
    int value;

    // connect each node to next node
    Node next;

    Node(int data) {
      value = data;
      next = null;
    }
  }

  public static void main(String[] args) {

    // create an object of LinkedList
    LL linkedList = new LL();

    // assign values to each linked list node
    linkedList.head = new Node(1);
    Node second = new Node(2);
    Node third = new Node(3);

    // connect each node of linked list to next node
    linkedList.head.next = second;
    second.next = third;

    // printing node-value
    System.out.print("The LinkedList formed is: ");
    while (linkedList.head != null) {
      System.out.print(linkedList.head.value + " ");
      linkedList.head = linkedList.head.next;
    }
  }
}

 

 

In the above provided example, the linked list comprises three nodes, where each node is composed of two components: a value and a reference to the next node.

The value variable within a node represents the specific value stored in that node, while the next variable serves as a pointer or link to the subsequent node in the sequence.

This structure allows for a sequential arrangement of nodes, enabling efficient traversal and manipulation of data within the linked list.

According to the above code, 1—>2—>3 will be the output. Node 1 will be the head and node 3 will be the tail of the LinkedList.

 

This is how LinkedList will look like:

 

 

 

Hope you enjoyed reading

Keep learning!

Now what?

Follow us on MADAlgos

 


Check the following blog for LinkedList

Introduction to LinkedList

https://madalgos.in/blog-space/27

https://madalgos.in/blog-space/28