# 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.

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

1. 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.

2. 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.

3. 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.

4. 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.

5. 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.

6. 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; };

}

void insert(int data);
void remove(int data);
void printList();
};

Node* newNode = new Node;
newNode->data = data;
}

Node* previous = nullptr;
while (current != nullptr) {
if (current->data == data) {
if (previous != nullptr) {
previous->next = current->next;
}
else {
}
delete current;
return;
}
previous = current;
current = current->next;
}
}

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.

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

1. 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.

2. 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.

3. 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.

4. 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.

5. Graphs: Linked lists can be used to implement graph algorithms such as depth-first search and breadth-first search.

6. 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.

7. 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:

1. 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.

2. 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.

3. 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.

4. 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.

5. 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.

6. 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.

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

2. 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.

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

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

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

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