Back to all blogs

MadAlgos Blog

Basic Operations on LinkedList ep01

M
Mansha Srivastava19 May 2023
Basic Operations on LinkedList ep01

A joke to begin with…

Why did the LinkedList break up with the Array?

Because the LinkedList wanted to keep inserting and deleting elements, but the Array just couldn't handle the constant "shift"!

hahaha… 😂

 

In Java, a LinkedList is a dynamic data structure that allows for efficient insertion and deletion of elements.

It consists of nodes linked together, with each node containing data and a reference to the next node.

By customizing our implementation, we can insert elements at the beginning of the LinkedList by creating a new node and updating the head reference.

Similarly, for deletion, we handle different cases such as deleting the head, middle, or tail nodes by updating the appropriate references. Understanding these concepts helps us manipulate LinkedLists effectively.

In this article, we will delve into the topic of inserting and deleting elements in a LinkedList in Java.

 

Inserting elements in LinkedList 


A LinkedList is a collection of nodes, where each node contains a data element and a reference to the next node in the list.

The first node is called the head, and the last node is called the tail. The LinkedList allows for dynamic resizing and efficient element insertion and deletion.

Inserting Elements in a LinkedList:

To insert an element in a LinkedList, we need to follow a few steps:

  1. Create a new node with the desired data.
  2. Set the reference of the new node to the current head.
  3. Update the head reference to point to the new node.

 

Here's the Java code for inserting an element at the beginning of a LinkedList:

 

public class LinkedList {
    private Node head;

    public void insertAtBeginning(int data) {
        Node newNode = new Node(data);
        newNode.next = head;
        head = newNode;
    }
}

 

In the above code, we define a LinkedList class with a private inner class Node that represents a single node in the list. The insertAtBeginning method creates a new node, sets its next reference to the current head, and updates the head to point to the new node.

 

Homework

Learn about inserting elements in LinkedList in the middle and the end

 

Deleting elements in LinkedList


To delete an element from a LinkedList, we need to consider different cases:

  1. Deleting the head node: In this case, we simply update the head reference to the next node.
  2. Deleting a node in the middle: We need to find the node before the one we want to delete and update its next reference to the node after the one we want to delete.
  3. Deleting the tail node: We need to find the node before the tail and update its next reference to null.

 

Here's the Java code for deleting an element from a LinkedList:

 

public class LinkedList {

    public void delete(int data) {
        if (head == null)
            return;
        
        if (head.data == data) {
            head = head.next;
            return;
        }

        Node curr = head;
        Node prev = null;

        while (curr != null && curr.data != data) {
            prev = curr;
            curr = curr.next;
        }

        if (curr == null)
            return;

        prev.next = curr.next;
    }
}

 

In the delete method above, we first handle the case of deleting the head node separately.

If the head node's data matches the desired data, we update the head reference to the next node and return.

Otherwise, we traverse the LinkedList until we find the node with the desired data. Once found, we update the previous node's next reference to skip the current node, effectively removing it from the list.

 

Homework

Learn about deleting elements in LinkedList in the middle and the end

 

In a nutshell, mastering the art of inserting and deleting elements in a LinkedList in Java gives you the power to twist and turn your data like a pro.

By customizing your code and getting your hands dirty, you'll become the king or queen of dynamic data manipulation.

Whether you're smoothly inserting elements at the beginning or gracefully deleting nodes from different positions, you'll be the boss of your LinkedList game.

So, dive in, experiment, and let your LinkedList dance to your Java tunes!

 

Hope you enjoyed reading

Dance with your LinkedList tunes!

Happy 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

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