👁 Preview — Study, Practice and Revise are open; mock tests and the rest of the syllabus unlock on subscription. Unlock all · ₹4,999
← Back to Linear Data Structures
Study mode

Linked lists (singly, doubly, circular)

Introduction to Linked Lists

In data structures, a linear data structure is one where elements are arranged sequentially, one after another. The most familiar example is an array, where elements are stored in contiguous memory locations. Arrays allow quick access to elements by their index, but they come with some limitations:

  • Fixed size: Once declared, the size of an array cannot be changed. If you need more space, you must create a new array and copy elements, which is inefficient.
  • Memory wastage or shortage: If the array size is too large, memory is wasted; if too small, it cannot hold all data.
  • Insertion and deletion: Adding or removing elements in the middle requires shifting many elements, which is time-consuming.

To overcome these problems, we use linked lists, a dynamic data structure that allows flexible memory usage and efficient insertions and deletions.

A linked list is made up of nodes. Each node contains two parts:

  1. Data: The actual information stored (e.g., an integer, a name, or any data).
  2. Pointer (or link): A reference to the next node in the list.

Unlike arrays, nodes in a linked list can be scattered anywhere in memory. The pointer connects them logically, forming a chain. This allows the list to grow or shrink dynamically as needed.

Think of a linked list like a treasure hunt: each clue (node) tells you where to find the next clue, rather than having all clues laid out in a row.

Singly Linked List

A singly linked list is the simplest form of linked list. Each node contains data and a pointer to the next node only. The list starts with a special pointer called the head, which points to the first node. The last node points to null, indicating the end of the list.

Data Next Data Next Data Next Null Head

Each node points only to the next node, so traversal is unidirectional, from head to tail.

Doubly Linked List

A doubly linked list extends the singly linked list by adding a second pointer in each node. This pointer points to the previous node, allowing traversal in both directions - forward and backward.

This bidirectional feature makes certain operations, like deletion or reverse traversal, more efficient.

Prev Data Next Prev Data Next Prev Data Next Null Head

Circular Linked List

A circular linked list is a variation where the last node points back to the first node, forming a loop. This can be implemented with singly or doubly linked lists.

In a singly circular linked list, the last node's next pointer points to the head node instead of null. In a doubly circular linked list, the last node's next points to the head, and the head's previous points to the last node.

This circular structure is useful for applications requiring continuous cycling through data, such as round-robin scheduling or buffering.

Data 1 Data 2 Data 3 Data 4 Head

Basic Operations on Linked Lists

Linked lists support three fundamental operations:

  • Insertion: Adding a new node at the beginning, end, or a specific position.
  • Deletion: Removing a node from the beginning, end, or a specific position.
  • Traversal: Visiting each node to read or process data.

These operations involve updating pointers carefully to maintain the list's structure.

graph TD    Start[Start Operation] --> CheckType{Insertion or Deletion?}    CheckType -->|Insertion| InsertPos{Position?}    InsertPos -->|Beginning| InsertBegin[Update head pointer]    InsertPos -->|End| InsertEnd[Traverse to last node, update pointer]    InsertPos -->|Middle| InsertMiddle[Traverse to position, update pointers]    CheckType -->|Deletion| DeletePos{Position?}    DeletePos -->|Beginning| DeleteBegin[Update head pointer]    DeletePos -->|End| DeleteEnd[Traverse to second last node, update pointer]    DeletePos -->|Middle| DeleteMiddle[Traverse to position, update pointers]    InsertBegin --> EndOp[End]    InsertEnd --> EndOp    InsertMiddle --> EndOp    DeleteBegin --> EndOp    DeleteEnd --> EndOp    DeleteMiddle --> EndOp

Worked Examples

Example 1: Insertion at the Beginning of a Singly Linked List Easy
Given a singly linked list with nodes containing data [10, 20, 30], insert a new node with data 5 at the beginning.

Step 1: Create a new node with data = 5.

Step 2: Set the new node's next pointer to the current head node (which contains 10).

Step 3: Update the head pointer to point to the new node.

Result: The list now starts with 5, followed by 10, 20, 30.

Example 2: Deletion of a Node in a Doubly Linked List Medium
In a doubly linked list with nodes [10 <> 20 <> 30 <> 40], delete the node containing 30.

Step 1: Traverse to the node with data 30.

Step 2: Update the previous node's (20) next pointer to point to 30's next node (40).

Step 3: Update the next node's (40) previous pointer to point to 30's previous node (20).

Step 4: Remove the node 30 from memory (optional in some languages).

Result: The list becomes [10 <> 20 <> 40].

Example 3: Traversal of a Circular Linked List Medium
Given a circular singly linked list with nodes containing [10, 20, 30, 40], write the steps to traverse and print all nodes without infinite looping.

Step 1: Start from the head node (10).

Step 2: Print the data of the current node.

Step 3: Move to the next node.

Step 4: Repeat steps 2 and 3 until you reach the head node again.

Note: Use a do-while loop to ensure the head node is processed before checking the condition.

Example 4: Comparing Memory Usage: Array vs Linked List Hard
Calculate and compare the memory required to store 1000 integers in an array and a singly linked list. Assume each integer uses 4 bytes and each pointer uses 8 bytes. Also, estimate the cost in INR if 1 byte costs Rs.0.05.

Step 1: Memory for array = number of elements x size of each element

\( M_{array} = 1000 \times 4 = 4000 \text{ bytes} \)

Step 2: Memory for singly linked list = number of nodes x (data size + pointer size)

\( M_{list} = 1000 \times (4 + 8) = 1000 \times 12 = 12000 \text{ bytes} \)

Step 3: Cost for array memory:

\( \text{Cost}_{array} = 4000 \times 0.05 = Rs.200 \)

Step 4: Cost for linked list memory:

\( \text{Cost}_{list} = 12000 \times 0.05 = Rs.600 \)

Conclusion: Arrays use less memory and cost less, but linked lists offer dynamic size and easier insertions/deletions.

Example 5: Implementing a Queue Using a Singly Linked List Medium
Show how to perform enqueue (insertion at end) and dequeue (deletion from beginning) operations on a queue implemented with a singly linked list.

Enqueue Operation:

Step 1: Create a new node with the data to be enqueued.

Step 2: If the queue is empty (head is null), set head and tail to the new node.

Step 3: Otherwise, set the current tail's next pointer to the new node.

Step 4: Update the tail pointer to the new node.

Dequeue Operation:

Step 1: Check if the queue is empty (head is null). If yes, report underflow.

Step 2: Store the data from the head node.

Step 3: Update the head pointer to the next node.

Step 4: If head becomes null, also set tail to null.

Step 5: Return the stored data.

Formula Bank

Memory Calculation for Linked List
\[ M = n \times (D + P) \]
where: \(M\) = total memory (bytes), \(n\) = number of nodes, \(D\) = size of data (bytes), \(P\) = size of pointer (bytes)
Traversal Time Complexity
\[ T(n) = O(n) \]
where: \(n\) = number of nodes
Insertion/Deletion Time Complexity
\[ T = O(1) \text{ (at head)}, \quad O(n) \text{ (at arbitrary position)} \]
where: \(n\) = number of nodes

Tips & Tricks

Tip: Always update pointers in the correct order during insertion or deletion to avoid losing access to nodes.

When to use: When performing insertion or deletion operations in linked lists.

Tip: Use a dummy head node to simplify edge cases and avoid null pointer exceptions.

When to use: To simplify code logic in linked list operations.

Tip: For circular linked lists, use a do-while loop to ensure all nodes are visited exactly once without infinite loops.

When to use: When traversing circular linked lists.

Tip: Remember that linked lists do not support direct access by index; always traverse sequentially.

When to use: When accessing elements by position in linked lists.

Tip: Visualize linked list operations with diagrams before coding to reduce logical errors.

When to use: Before implementing linked list algorithms in exams or coding.

Common Mistakes to Avoid

❌ Not updating the head pointer after insertion at the beginning.
✓ Always assign the new node as the head after insertion.
Why: Forgetting to update the head causes loss of access to the entire list.
❌ Breaking the chain by incorrect pointer assignment during deletion.
✓ Update previous and next pointers carefully to maintain list integrity.
Why: Confusion about pointer updates leads to broken links and data loss.
❌ Infinite loops when traversing circular linked lists without proper termination condition.
✓ Use a do-while loop and stop traversal when the starting node is reached again.
Why: Missing the circular nature causes infinite looping.
❌ Assuming linked lists allow direct access by index like arrays.
✓ Understand that linked lists require sequential traversal to access nodes.
Why: Misconception from arrays causes inefficient or incorrect code.
❌ Forgetting to handle empty list cases during insertion or deletion.
✓ Check if the list is empty (head == null) before performing operations.
Why: Null pointer exceptions or runtime errors occur if empty cases are ignored.
FeatureSingly Linked ListDoubly Linked ListCircular Linked List
Node StructureData + Next pointerData + Prev + Next pointersLast node points to first node
TraversalOne direction (forward)Two directions (forward & backward)Continuous loop traversal
Insertion/DeletionEasy at head, harder elsewhereEasier anywhere due to prev pointerSimilar to singly/doubly but circular
Memory UsageLess (one pointer)More (two pointers)Same as singly/doubly but circular
Use CasesSimple lists, stacksComplex lists, undo operationsRound-robin, buffering
Curated videos per subtopic
Top YouTube explainers, AI-ranked for your exam and language. Unlocks with subscription.
Unlock

Try Practice next.

Progress tracking is paywalled — subscribe to mark subtopics as understood and save your streak.

Go to practice →
Ask a doubt
Linked lists (singly, doubly, circular) · 10 free messages
Ask me anything about this subtopic. You have 10 free messages this session — chat history isn't saved in preview.