What is a linked list?

Linked list

A linked list is a common data structure made of one or more than one nodes. Each node contains a value and a pointer to the previous/next node forming the chain-like structure. These nodes are stored randomly in the system's memory, which improves its space complexity compared to the array.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Types of linked list

  • Singly-linked list

  • Doubly linked list

  • Circular linked list

In this Answer, we'll learn about the singly linked list and its operations.

Hint: Have you ever played Treasure Hunt? It works same as linked list.

Working

We create a linked list by using classes. Here we implement a singly linked list. Let's see how to code a linked list.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

We make a node class that has the data attributes and a node pointer link that stores the address of the node.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

The head pointer points to the first node, and the last element of the list point to the null, which is the tail. When the list is empty, the head and tail the pointer points to NULL.

Operations

We perform the following functions on the linked list:

  • Insertion

  • Search

  • Deletion

Insertion

We can insert a node at the start as head, at the end as tail, or any desired position by entering the position number. Here, we insert the first element at the beginning to create the head and then the upcoming elements at the tail.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

In this code, the add_node function is used to insert nodes into the linked list. First, it checks whether the list exists or not. If yes, a new node is added to the list, and a head is created.

Search

We can search in the linked list by traversing all the elements starting from the head node.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

In this code, the search function is defined to search the given value p_data. We use while loop to traverse the linked list. After finding the value, the loop breaks down. If the loop is not broken, the entered value doesn't exist in the linked list.

Deletion

We can delete the head node, the tail, or at any desired position by first searching it. Here we delete the second node.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

In this code, delete_node is defined to delete the head, tail, or any node of a linked list. current and prev nodes are keeping track of the linked list.

  • Line 12–26: We delete the head note, and assign the next node as head.

  • Line 30–36: We delete the tail node, and assign the last node as tail.

  • Line 38–48: We set the current to the next node. If current it becomes empty, the given data p_data doesn't exist in the linked list.

This widget is not supported in dev-mode. Kindly enable it or run using yarn webapp:dev-widgets.

Free Resources