# What are Linked Lists in Data Structure?

A linked list is a linear collection of data elements, called nodes, each pointing to the next node by means of a pointer. It is a data structure consisting of a collection of nodes which together represent a sequence. In a singly linked list, each node stores a reference to an object and a reference to the next node in the list. In a doubly linked list, each node also stores a reference to the previous node. Linked lists are often used because of their efficient insertion and deletion operations. They can be used to implement other data structures such as stacks, queues, and associative arrays.

A linked list is a data structure that consists of a sequence of nodes, where each node contains two fields:

- An element of the list, also called the "data" or "value"
- A reference to the next node in the list, also called the "next pointer"

The first node in the list is called the "head" and the last node is called the "tail". The head node has a reference to the second node in the list, the second node has a reference to the third node, and so on. The last node in the list has a next pointer that points to null, indicating the end of the list.

## Types of Linked List

There are several types of linked lists, each with its own characteristics and use cases:

**Singly linked list:**A singly linked list is a basic linked list where each node has a reference to the next node in the list, but not to the previous node. It is useful for situations where you only need to traverse the list in one direction, such as a stack or a queue.**Doubly linked list:**A doubly linked list is an extension of the singly linked list where each node has a reference to both the next and the previous nodes in the list. It is useful for situations where you need to traverse the list in both directions, such as a deque or a hash table.**Circular linked list:**A circular linked list is a type of linked list where the last node points back to the first node, forming a loop. It is useful for situations where you need to traverse the list in a circular fashion, such as a circular buffer.**Skip list:**A skip list is a linked list with multiple levels, where each level contains a subset of the elements in the list. It is useful for situations where you need to search for elements in a list with a large number of items, as it can reduce the number of elements that need to be traversed.**XOR linked list:**A XOR linked list is a type of linked list where each node stores the memory address of the previous and next node using the XOR operation. It's a memory-efficient data structure, which is useful in situations where you have limited memory and need to traverse the list in both directions.**Multi-level linked list :**A multi-level linked list is a linked list where each node has a reference to one or more other linked lists. It is useful for situations where you need to organize elements in a hierarchical structure, such as a file system or a tree.

Each type of linked list has its own advantages and disadvantages, and choosing the right one depends on the specific requirements of the application.

## Example of Creating Linked Lists

A linked list can be implemented in many programming languages, but the basic structure is the same. Here are examples of how to implement a singly linked list in a few different languages:

```
struct Node {
int data;
Node* next; };
class LinkedList {
private: Node* head;
public: LinkedList() {
head = nullptr;
}
void insert(int data);
void remove(int data);
void printList();
};
void LinkedList::insert(int data) {
Node* newNode = new Node;
newNode->data = data;
newNode->next = head;
head = newNode;
}
void LinkedList::remove(int data) {
Node* current = head;
Node* previous = nullptr;
while (current != nullptr) {
if (current->data == data) {
if (previous != nullptr) {
previous->next = current->next;
}
else {
head = current->next;
}
delete current;
return;
}
previous = current;
current = current->next;
}
}
void LinkedList::printList() {
Node* current = head;
while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
}
```

The examples above show the basic structure of a singly linked list and its common operations such as insert, remove, and print the list. The implementation may vary depending on the programming language, but the overall concept is similar.

## Application of Linked Lists

Linked lists are a versatile data structure that can be used in many different types of applications. Some common examples include:

**Stacks:**A stack is a Last In First Out (LIFO) data structure, and a singly linked list can be used to implement it efficiently. The head of the list represents the top of the stack, and push and pop operations can be implemented using insert and remove operations on the head of the list.**Queues:**A queue is a First In First Out (FIFO) data structure, and a singly linked list can also be used to implement it. The head of the list represents the front of the queue, and enqueue and dequeue operations can be implemented using insert and remove operations on the tail of the list.**Hash tables:**A hash table is a data structure that allows for efficient searching, insertion, and deletion of elements based on a key. A doubly linked list can be used to implement a hash table, where each element is stored in a node along with its key and the nodes are arranged in a linked list based on their hash values.**Memory management:**Linked lists can be used to manage memory dynamically. In this case, a singly linked list of free memory blocks is maintained, and when a new block is needed, it is taken from the front of the list. When a block is freed, it is added to the front of the list.**Graphs:**Linked lists can be used to implement graph algorithms such as depth-first search and breadth-first search.**Polynomial:**Linked lists are used to represent polynomials in computer science. Each node represents a term in a polynomial and contains two fields: coefficient and exponent.**Database:**Linked lists are used in databases to implement indexes.

These are some of the common application of linked lists, however, it has many other applications as well, and it's an important data structure to understand as it's building block for other data structure.

## Time and Space Complexity of Linked Lists

The time and space complexity of linked lists depend on the operations that are performed on them. Here are the average time and space complexities for some common operations:

**Insertion at the head:**O(1) for both time and space complexity. The operation only requires creating a new node and updating the head pointer, which is a constant time operation.**Insertion at the tail:**O(n) for time complexity and O(1) for space complexity in a singly linked list. The operation requires traversing the list to find the tail, which takes linear time, but only requires creating a new node, which is a constant space operation. In a doubly linked list, it's O(1) for time complexity as well as space complexity.**Deletion at the head:**O(1) for both time and space complexity. The operation only requires updating the head pointer and deleting the current head node, which are both constant time operations.**Deletion at the tail:**O(n) for time complexity and O(1) for space complexity in a singly linked list. The operation requires traversing the list to find the tail, which takes linear time, but only requires deleting the tail node, which is a constant space operation. In a doubly linked list, it's O(1) for time complexity as well as space complexity.**Searching:**O(n) for time complexity and O(1) for space complexity. The operation requires traversing the list to find the desired element, which takes linear time, but only requires a single variable to store the current node, which is a constant space operation.**Accessing:**O(n) for time complexity and O(1) for space complexity. The operation requires traversing the list to find the desired element, which takes linear time, but only requires a single variable to store the current node, which is a constant space operation.

It's worth noting that these complexities are the average case, linked list can be optimized for certain operation and the complexities can be improved.

## Advantages and Disadvantages of Linked Lists

Linked lists have several advantages and disadvantages as a data structure:

**Advantages:**

They are efficient for inserting and deleting elements, as the only operation required is to update the pointers of the surrounding nodes.

They use less memory than arrays, since they only need to store the elements and the pointers, rather than allocating a contiguous block of memory for all the elements.

They can be easily expanded or contracted as needed, without the need to reallocate memory or copy elements.

They can be useful in situations where the size of the data set may change frequently.

They are useful in implementing other data structures like stack, queue, and associative arrays.

They can be used to implement certain data structures more efficiently, such as hash tables and memory management.

**Disadvantages:**

They are less efficient for random access and searching, as each element must be traversed sequentially starting from the head.

They are more complex to implement and maintain than arrays or other data structures.

They can be less cache-friendly than arrays, as the elements are not stored in a contiguous block of memory.

They can consume more memory than arrays due to the extra storage required for the pointers.

They can be less efficient for certain operations, such as finding the middle element of a list.

Overall, linked lists are a powerful data structure that can be useful in certain situations. However, it's important to consider the specific requirements of an application and choose the appropriate data structure to meet those requirements.