Linked Lists in Data Structures: Complete Guide with Types and Examples

Introduction to Linked Lists

A linked list is a linear data structure in which elements are stored in separate objects called nodes. Each node contains data and a reference or pointer to another node. Unlike arrays, linked list elements are not required to be stored in consecutive memory locations.

Linked lists are an important topic in Data Structures and Algorithms because they help developers understand dynamic memory allocation, pointers, and efficient insertion and deletion operations.

For example, imagine a music playlist containing multiple songs. Each song can be connected to the next song. This connection represents the basic idea of a linked list.

What Is a Node in a Linked List?

A node is the basic building block of a linked list. A typical node contains two parts:

  1. Data: Stores the actual value.
  2. Next pointer: Stores the address of the next node.

The first node is called the head. The last node usually points to NULL, which indicates the end of the list.

Example representation:

text
Head
 |
 v
[10 | next] -> [20 | next] -> [30 | NULL]

In this example, the list contains three nodes with values 10, 20, and 30.

Types of Linked Lists

Linked lists are commonly divided into four types.

1. Singly Linked List

A singly linked list is the simplest type. Every node contains data and a pointer to the next node. Traversal is possible only in the forward direction.

Example:

text
10 -> 20 -> 30 -> NULL

Advantages:
- Simple structure.
- Uses memory dynamically.
- Insertion and deletion at the beginning are efficient.

Disadvantages:
- Cannot move backward.
- Searching requires sequential traversal.
- Each node stores an additional pointer.

2. Doubly Linked List

A doubly linked list contains two pointers in each node. One pointer connects to the previous node, and another connects to the next node.

Example:

text
NULL <- 10 <-> 20 <-> 30 -> NULL

Advantages:
- Supports forward and backward traversal.
- Deletion can be easier when a node reference is available.
- Useful for navigation systems and browser history.

Disadvantages:
- Requires additional memory.
- Pointer management is more complex.

3. Circular Linked List

In a circular linked list, the last node connects back to the first node instead of pointing to NULL.

Example:

text
10 -> 20 -> 30
^           |
|___________|

Advantages:
- Traversal can continue in a circular manner.
- Useful in round-robin scheduling.
- Useful for repeating playlists and cyclic processes.

Disadvantages:
- Traversal needs a stopping condition.
- Incorrect conditions can create infinite loops.

4. Circular Doubly Linked List

A circular doubly linked list combines the features of doubly linked and circular linked lists. Every node has previous and next pointers, and the first and last nodes are connected in both directions.

How to Implement a Linked List in C++

The following example creates a basic singly linked list using a structure and pointers.

```cpp
#include <iostream>
using namespace std;

struct Node {
int data;
Node* next;
};

int main() {
Node* first = new Node();
Node* second = new Node();
Node* third = new Node();

first->data = 10;
first->next = second;

second->data = 20;
second->next = third;

third->data = 30;
third->next = nullptr;

Node* current = first;

while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}

delete third;
delete second;
delete first;

return 0;
}
```

Output:

text
10 20 30

Here, three nodes are created dynamically. Each node stores a value and the address of the next node. The traversal loop visits every node until it reaches nullptr.

Basic Operations on Linked Lists

Linked lists support several important operations. Understanding these operations is essential for solving DSA problems.

1. Traversal

Traversal means visiting every node from the head to the end of the list.

```cpp
void display(Node* head) {
Node* current = head;

while (current != nullptr) {
cout << current->data << " ";
current = current->next;
}
}
```

Time Complexity: O(n)

2. Insertion at the Beginning

A new node can be inserted at the beginning by connecting it to the current head and then updating the head pointer.

```cpp
void insertAtBeginning(Node*& head, int value) {
Node* newNode = new Node();

newNode->data = value;
newNode->next = head;

head = newNode;
}
```

Time Complexity: O(1)

3. Insertion at the End

To insert a node at the end, first create a new node. Then traverse the list until the last node and connect it to the new node.

```cpp
void insertAtEnd(Node*& head, int value) {
Node* newNode = new Node();

newNode->data = value;
newNode->next = nullptr;

if (head == nullptr) {
head = newNode;
return;
}

Node* current = head;

while (current->next != nullptr) {
current = current->next;
}

current->next = newNode;
}
```

Time Complexity: O(n)

4. Deletion from the Beginning

To delete the first node, move the head pointer to the next node and release the old node from memory.

```cpp
void deleteFromBeginning(Node*& head) {
if (head == nullptr) {
return;
}

Node* temp = head;
head = head->next;

delete temp;
}
```

Time Complexity: O(1)

5. Searching an Element

Searching means finding whether a particular value exists in the linked list.

```cpp
bool search(Node* head, int target) {
Node* current = head;

while (current != nullptr) {
if (current->data == target) {
return true;
}

current = current->next;
}

return false;
}
```

Time Complexity: O(n)

Linked List Time Complexity

The time complexity of common singly linked list operations is shown below.

| Operation | Time Complexity |
|---|---|
| Access by index | O(n) |
| Search | O(n) |
| Traversal | O(n) |
| Insert at beginning | O(1) |
| Insert at end | O(n) |
| Delete at beginning | O(1) |
| Delete at end | O(n) |

These complexities assume a basic singly linked list with only a head pointer. Maintaining a tail pointer can make insertion at the end O(1).

Array vs Linked List

Arrays and linked lists are both linear data structures, but they work differently.

| Feature | Array | Linked List |
|---|---|---|
| Memory allocation | Usually contiguous | Non-contiguous |
| Size | Often fixed after creation | Dynamic |
| Random access | O(1) | O(n) |
| Search | O(n) | O(n) |
| Insert at beginning | O(n) | O(1) |
| Extra pointer memory | Not required per element | Required |

Arrays are generally useful when fast index-based access is important. Linked lists are useful when dynamic size and frequent insertion or deletion are important.

Real-Life Applications of Linked Lists

Linked lists are used in several computer science applications:

  1. Browser history: Doubly linked lists can support forward and backward navigation.
  2. Music playlists: Songs can be connected to the next or previous song.
  3. Operating systems: Linked structures can be used in memory management and scheduling implementations.
  4. Undo and redo systems: Previous and next states can be connected.
  5. Graph representation: Adjacency lists can store connected vertices.
  6. Hash tables: Linked lists can help handle collisions through separate chaining.

Common Linked List Interview Questions

Students preparing for coding interviews should practise these problems:

  1. Reverse a linked list.
  2. Find the middle node of a linked list.
  3. Detect a cycle in a linked list.
  4. Find the nth node from the end.
  5. Merge two sorted linked lists.
  6. Remove duplicates from a sorted linked list.
  7. Check whether a linked list is a palindrome.
  8. Find the intersection point of two linked lists.
  9. Delete a node without using the head pointer when possible.
  10. Sort a linked list using merge sort.

Advantages and Disadvantages of Linked Lists

Advantages

  • Dynamic memory allocation.
  • Efficient insertion and deletion at known positions.
  • No requirement for contiguous memory.
  • Useful for implementing stacks, queues, and other data structures.

Disadvantages

  • Extra memory is required for pointers.
  • No direct random access.
  • Traversal can be slower than array indexing.
  • Pointer errors can cause memory leaks or invalid memory access.

Tips to Learn Linked Lists

If you are learning linked lists for the first time, follow this sequence:

  1. Understand nodes, pointers, and the head.
  2. Create a simple singly linked list.
  3. Practise traversal and searching.
  4. Learn insertion and deletion.
  5. Understand doubly and circular linked lists.
  6. Solve reversal and middle-node problems.
  7. Practise linked list questions on coding platforms.

Draw each node on paper and trace how pointers change during every operation. This makes linked list concepts much easier to understand.

Conclusion

Linked lists are a fundamental data structure in DSA. They provide dynamic memory management and allow efficient insertion and deletion at specific positions when the required node references are available.

Understanding singly, doubly, and circular linked lists builds a strong foundation for advanced topics such as stacks, queues, graphs, and linked list interview problems. Practise the operations with C++ examples and focus on pointer connections to develop strong problem-solving skills.

✓

You’ve reached the end of this guide. Explore more tutorials on PrimeProblem.