👋 Welcome back, coding explorer!
Last week, we explored Strings, the sentences of code that hold meaning and patterns.
This week in DSA Bites, we’re venturing into one of the most fundamental and visual data structures, the Linked List.
At first, it might feel a little tricky, but don’t worry, we’ll break it down into easy, bite-sized pieces.
Part 1: What is a Linked List?
A Linked List is a collection of connected elements called nodes.
Each node contains two things:
1️⃣ The data itself
2️⃣ A pointer (or link) to the next node
💡 Think of it as a train, where each car (node) carries data and knows where the next car is.
[10 | *] → [20 | *] → [30 | None]
Each car is linked — forming a chain of data.
The last one points to None — marking the end of the journey.
Part 2: Why Linked Lists?
Arrays are great, but they come with limitations:
❌ Fixed size
❌ Costly insertions and deletions in the middle
Linked Lists fix these by being dynamic.
You can add or remove elements without shifting everything.
They give you flexibility at the cost of some extra links.
Part 3: Types of Linked Lists
Type | Description | Visual |
|---|---|---|
Singly Linked List | Each node points to the next one only |
|
Doubly Linked List | Nodes point both ways (next & previous) |
|
Circular Linked List | Last node links back to the first |
|
Part 4: Creating a Linked List
class Node:
def __init__(self, data):
self.data = data
self.next = None
# Create nodes
a = Node(10)
b = Node(20)
c = Node(30)
# Link them
a.next = b
b.next = c
# Traverse
current = a
while current:
print(current.data)
current = current.next
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
let a = new Node(10);
let b = new Node(20);
let c = new Node(30);
a.next = b;
b.next = c;
let current = a;
while (current) {
console.log(current.data);
current = current.next;
}
Output:
10
20
30
Part 5: Common Linked List Operations
Traversing a Linked List
Move through every node one by one:
current = head
while current:
print(current.data)
current = current.next
let current = head;
while (current) {
console.log(current.data);
current = current.next;
}
Inserting a Node (at the end)
new_node = Node(40)
current = head
while current.next:
current = current.next
current.next = new_node
let newNode = new Node(40);
let current = head;
while (current.next) current = current.next;
current.next = newNode;
Deleting a Node
temp = head
while temp.next.data != 20:
temp = temp.next
temp.next = temp.next.next
let temp = head;
while (temp.next.data !== 20) temp = temp.next;
temp.next = temp.next.next;
Part 6: Common Interview Patterns
✅ Reverse a Linked List
prev = None
current = head
while current:
next_node = current.next
current.next = prev
prev = current
current = next_node
head = prev
let prev = null, current = head;
while (current) {
let nextNode = current.next;
current.next = prev;
prev = current;
current = nextNode;
}
head = prev;
✅ Detect a Cycle (Floyd’s Algorithm)
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
print("Cycle detected")
break
let slow = head, fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
if (slow === fast) {
console.log("Cycle detected");
break;
}
}
✅ Find Middle Node
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
print("Middle:", slow.data)
let slow = head, fast = head;
while (fast && fast.next) {
slow = slow.next;
fast = fast.next.next;
}
console.log("Middle:", slow.data);
Part 7: Mini Challenges
1️⃣ Count Nodes in a Linked List
➡️ Input: 10 → 20 → 30
➡️ Output: 3
2️⃣ Find the Middle Node
➡️ Input: 1 → 2 → 3 → 4 → 5
➡️ Output: 3
3️⃣ Reverse a Linked List
➡️ Input: 1 → 2 → 3
➡️ Output: 3 → 2 → 1
4️⃣ Detect a Loop
➡️ Input: 1 → 2 → 3 → (points back to 2)
➡️ Output: True
💪 Expert Challenge – Merge Two Sorted Linked Lists
List1: 1 → 3 → 5
List2: 2 → 4 → 6
Result: 1 → 2 → 3 → 4 → 5 → 6
🤖 AI Tip
Try asking AI:
“Visualize how linked lists connect in memory and generate 5 practice problems on reversing, merging, and cycle detection.”
AI can help you visualize node movement step-by-step.
✅ Sign-off
That’s your Linked Lists Bite – Beginner to Expert! 🎉
You now understand how Linked Lists store data dynamically and pave the way for Stacks, Queues, and Graphs.
Next up: Stacks & Queues, where we see Linked Lists in action through browser history, undo operations, and task scheduling!
Keep coding and stay curious,
Fahim | DSA Bites.