Nishant Rana
Last Updated: Mar 23, 2023
MEDIUM

## Introduction

LinkedList is a linear data structure that is used to store the data. Unlike arrays, data is not continuously stored in the LinkedList. Each data is linked to the other using pointers. Refer to the below image for more understanding.

Recommended Topic, Floyds Algorithm

Now, let's look at the types of linked list.

A singly linked list is a data structure that consists of a series of nodes, where each node stores a value (or data) and a reference (or pointer) to the immediate next node of the series. The first node of the series is called the head, and the last node of the series points to a null reference.

Here is an example of a singly linked list with four nodes, where each node contains an integer value:

``head -> [5] -> [3] -> [7] -> [2] -> null``

In this example, the head node contains the value 5, and it points to the next node in the sequence, which contains the value 3. The third node contains the value 7, and it points to the last node in the sequence, which contains the value 2 and points to a null reference.

### Doubly or Two Way Linked List

A doubly linked list, also known as a two-way linked list, is a data structure that is the same as a singly linked list, except that each node in the sequence contains a reference to both the next and the previous nodes in the sequence. This allows for efficient traversal of the list in both directions, from the head to the tail and from the tail to the head.

Here is an example of a doubly linked list with four nodes, where each node contains an integer value:

``````head <-> [5] <-> [3] <-> [7] <-> [2] <-> null
In this example, the head node contains the value 5 and points to the next node in the sequence, which contains the value 3 and also points back to the previous node. Similarly, the third node contains the value 7 and points to the next node in the sequence, which contains the value 2 and also points back to the previous node.``````

A circular linked list is a slight variation of the singly linked list, in which the last node in the sequence points back to the first node, forming a loop or circle. In other words, the next pointer of the last node of the series points to the head of the list.

``````Here is an example of a circular linked list with four nodes, where each node contains an integer value:
head -> [5] -> [3] -> [7] -> [2] -|
^                                 |
|---------------------------------|``````

In this example, the head node contains the value 5, and it points to the next node in the sequence, which contains the value 3. The third node contains the value 7, and it points to the last node in the sequence, which contains the value 2 and points back to the head node.

A circular doubly linked list is a data structure that consists of a series of nodes, where each node stores a value, a reference to the immediate next node in the sequence, and a reference to the previous node of the series. The last node of the series points to the first node, creating a circular sequence.

Here is an example of a circular doubly linked list with four nodes, where each node contains an integer value:

``head -> [5] <-> [3] <-> [7] <-> [2] <-> head``

In this example, the head node contains the value 5, and it points to the next node in the sequence, which contains the value 3 and points back to the head node. The third node contains the value 7, and it points to the next node in the sequence, which contains the value 2 and points back to the head node.

A header linked list is a variation of a singly linked list that includes a header node at the beginning of the list. The header node does not stores any data and serves as a sentinel or marker for the beginning of the list. The header node always exists, even if the list is empty.

Here is an example of a header linked list with four nodes:

``header -> [5] -> [3] -> [7] -> [2] -> null``

In this example, the header node does not contain any data and serves only as a marker for the beginning of the list. The first node in the sequence contains the value 5, and it points to the next node in the sequence, which contains the value 3. The third node contains the value 7, and it points to the last node in the sequence, which contains the value 2 and points to a null reference.

1. Dynamic Data Structure: In LinkedList, memory is dynamically allocated to the LinkedList. One can easily add or remove an element to the LinkedList at the runtime. Hence, there is no need for initial size.
2. Implementation: LinkedList is a very useful Data Structure that helps implement other Data Structures like Stacks and Queues.
3. No Memory Wastage: As discussed in the first point, memory is dynamically allocated to the LinkedList. That is why we can remove the memory which is not in use, and no memory is wasted.
4. Insertion and Deletion: In LinkedList, insertion and deletion operations are efficient compared to other Data Structures such as Arrays as no shifting of elements is required in the LinkedList. Only the pointers need to be updated.
5. Versatility: Linked lists can be used to implement a wide range of data structures, including stacks, queues, associative arrays, and graphs, as well as linked data structures such as trees and hash tables.
6. Persistence: Linked lists can be used to implement persistent data structures, which are data structures that can be modified and accessed across multiple program executions. This is because linked lists can be easily serialized and stored in non-volatile memory.

1. Memory Usage: The memory used by LinkedList is more because we also need to store the address of the next data.
2. Accessing an element: We can not access any element of the LinkedList directly. We don’t have direct access to every element of LinkedList. If we want to access the ith element of the LinkedList, then we need to traverse the LinkedList till the ith index.
3. Reverse Traversal: The reverse traversal is not possible in the Singly LinkedList because we don’t have the memory address of the previous pointers. It is possible in the Doubly LinkedList, but again it consumes more memory as we need to store the memory address of the previous pointers.
4. More complex implementation: Linked lists can be more complex to implement than other data structures, such as arrays or stacks. They require knowledge of dynamic memory allocation and pointer manipulation, which can be difficult for novice programmers.
5. Lack of cache locality: Linked lists may not take advantage of the caching mechanisms in modern processors, since accessing elements in a linked list requires jumping to different locations in memory. This can result in slower performance compared to other data structures that have better cache locality, such as arrays.

You can also read Difference Between Array List and Linked List here.

Linked lists have dynamic size allocation, allowing for efficient insertion and deletion of elements at any position, whereas arrays have fixed size allocation, requiring expensive memory reallocation and copying for insertions and deletions.

### What are the applications of linked list?

Linked lists have various applications, such as implementing dynamic data structures like stacks, queues, and hash tables. They are also used in memory allocation, graph traversal algorithms, and file systems. Linked lists are particularly useful when memory is limited or when inserting or deleting elements frequently.

### Can we access the random element of the LinkedList?

We can not access any element of the LinkedList directly. We don’t have direct access to every element of LinkedList. If we want to access the ith element of the LinkedList, then we need to traverse the LinkedList till the ith index.

### How is memory not wasted in LinkedList?

Since the memory is dynamically allocated to the LinkedList, we can remove the memory that is not in use, and no memory is wasted.

## Conclusion

In this blog, we have covered the following things:

1. We first discussed what LinkedList is.

Until then, All the best for your future endeavors, and Keep Coding.