Chapter 14: DSA Linked Lists Operations

DSA Linked List Operations – Let’s go through this topic thoroughly, like I’m teaching you face-to-face with a whiteboard, writing code step-by-step, showing memory changes, and explaining why each operation has the time complexity it has.

We will focus mainly on Singly Linked List operations (the most common in interviews), and I’ll mention differences for Doubly Linked List where they matter significantly.

Most Important Linked List Operations (in order of frequency in interviews)

  1. Traversal / Print
  2. Find length / size
  3. Insert at beginning
  4. Insert at end
  5. Insert after a given node / at a position
  6. Delete from beginning
  7. Delete from end
  8. Delete a given node (by value or by reference)
  9. Search for a value
  10. Reverse a linked list
  11. Find middle node
  12. Detect cycle (very famous)

Let’s go one by one with clear explanation, visual memory change, code, and time complexity.

1. Traversal / Print the list

What we do: Start from head and keep moving to next until we reach null.

Python

Time Complexity: O(n) Space Complexity: O(1)

2. Find length / size of linked list

Python

Time: O(n) Used very often as a helper function.

3. Insert at Beginning (very fast & very common)

Memory change (new node becomes new head):

Before: head → [10] → [25] → [7] → null

After insert 99: head → [99] → [10] → [25] → [7] → null

Python

Time: O(1) Very important: This is one of the biggest advantages of linked list over array.

4. Insert at End

Two versions:

Version A: Without tail pointer → O(n)

Python

Version B: With tail pointer → O(1) (recommended in good implementations)

Python

Time: O(1) with tail, O(n) without

5. Insert after a given node (or at position k)

Very common interview question

Python

Time: O(1) (if we already have pointer to the node after which we want to insert)

Insert at position k (1-based):

Python

Time: O(position) → worst case O(n)

6. Delete from Beginning

Python

Time: O(1)

7. Delete from End

Without tail → O(n)

Python

With tail → still O(n) unless you keep second-last pointer (rare)

Time: O(n) in singly linked list

8. Delete a node by value

Two famous versions:

A. Normal delete by value

Python

B. Delete without head pointer (very famous tricky question)

You are given only pointer to the node to be deleted (not head).

Trick: copy next node’s data to current and delete next node.

Python

Time: O(1)

9. Search for a value

Python

Time: O(n)

Quick Summary Table – Singly Linked List Operations

Operation Time Complexity Very Important?
Insert at beginning O(1) ★★★★★
Insert at end (with tail) O(1) ★★★★
Insert after given node O(1) ★★★★
Delete from beginning O(1) ★★★★
Delete from end O(n) ★★★
Delete given node (with ptr) O(1) ★★★★★ (tricky version)
Search / traversal O(n) ★★★★★
Reverse O(n) ★★★★★
Find middle O(n) ★★★★★
Detect cycle O(n) ★★★★★

Would you like to go deeper into any of these operations with full code + dry run?

Most requested next topics:

  • Reverse a linked list (iterative + recursive)
  • Find middle of linked list (fast & slow pointer)
  • Detect & remove cycle
  • Merge two sorted linked lists
  • Reverse in groups of k (hard but very asked)
  • Doubly linked list important operations

Just tell me which one you want next — I’ll explain with the same level of detail, visuals, and examples! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *