Circular vs Linear Linked List Key Difference in DSA Typescript - Complexity Comparison
We want to understand how the time to traverse linked lists changes depending on their type.
How does the structure affect the number of steps to visit nodes?
Analyze the time complexity of traversing linear and circular linked lists.
// Linear Linked List traversal
function traverseLinear(head: Node | null): void {
let current = head;
while (current !== null) {
// process current node
current = current.next;
}
}
// Circular Linked List traversal
function traverseCircular(head: Node | null): void {
if (!head) return;
let current = head;
do {
// process current node
current = current.next;
} while (current !== head);
}
These functions visit each node once in linear and circular linked lists respectively.
Both functions use loops to visit nodes repeatedly.
- Primary operation: Visiting each node once.
- How many times: Exactly once per node, total n times where n is list length.
As the list grows, the number of steps grows directly with the number of nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 visits |
| 100 | 100 visits |
| 1000 | 1000 visits |
Pattern observation: The steps increase linearly as the list size increases.
Time Complexity: O(n)
This means the time to traverse grows directly with the number of nodes in the list.
[X] Wrong: "Circular linked lists take longer because they loop forever."
[OK] Correct: The traversal stops after visiting all nodes once by checking when it returns to the start, so it does not loop endlessly.
Understanding how traversal time depends on list type helps you explain data structure choices clearly and confidently in interviews.
What if the circular linked list did not have a clear stopping condition? How would that affect traversal time complexity?
Practice
Solution
Step 1: Understand node connections in linear linked list
In a linear linked list, nodes are connected one after another, and the last node points to null, ending the list.Step 2: Understand node connections in circular linked list
In a circular linked list, the last node points back to the first node, creating a loop or circle.Final Answer:
In a circular linked list, the last node points back to the first node, forming a circle. -> Option AQuick Check:
Circular list loops back = In a circular linked list, the last node points back to the first node, forming a circle. [OK]
- Confusing which list loops back
- Thinking linear list forms a circle
- Assuming circular list has no end node
Solution
Step 1: Understand node pointer in circular list
In a circular linked list, the next pointer of the last node points back to the head node itself.Step 2: Check the condition for circular list
If head.next equals head, it means the list loops back to the start, confirming circularity.Final Answer:
if (head.next === head) { /* circular list */ } -> Option DQuick Check:
Next points to head means circular [OK]
- Checking for null instead of head
- Using undefined instead of null
- Confusing zero with null pointer
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
const head = new Node(1);
const second = new Node(2);
const third = new Node(3);
head.next = second;
second.next = third;
third.next = head;
let current = head;
let count = 0;
while (count < 5) {
console.log(current.value);
current = current.next!;
count++;
}
What will be the output printed?Solution
Step 1: Understand the circular linked list structure
The last node (third) points back to head, so the list loops: 1 -> 2 -> 3 -> 1 -> ...Step 2: Trace the loop for 5 iterations
Starting at head (1), the values printed are: 1, 2, 3, then loops back to 1, then 2.Final Answer:
1 2 3 1 2 -> Option BQuick Check:
Circular list repeats nodes in order [OK]
- Assuming list ends after 3 nodes
- Printing null after last node
- Confusing node values after loop
function isCircular(head: Node | null): boolean {
let current = head;
while (current !== null) {
if (current.next === head) {
return true;
}
current = current.next!;
}
return false;
}
What is the bug in this code?Solution
Step 1: Analyze the loop condition and pointer movement
The loop moves current to current.next until current is null or current.next equals head.Step 2: Identify the problem with circular lists
If the list is circular but the loop does not point directly back to head (e.g., cycle in middle), the loop never ends, causing infinite loop.Final Answer:
It may enter an infinite loop if the list is circular but not pointing directly to head. -> Option CQuick Check:
Loop may never end if cycle not at head [OK]
- Assuming only last node points to head
- Ignoring infinite loop possibility
- Confusing syntax error with logic error
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
const head = new Node(10);
const second = new Node(20);
const third = new Node(30);
head.next = second;
second.next = third;
third.next = null; // currently linear list
// Which line below makes the list circular?
// A: third.next = null;
// B: second.next = head;
// C: head.next = third;
// D: third.next = head;
Solution
Step 1: Understand the current list structure
The list is linear: head -> second -> third -> null.Step 2: Make the last node point to head to form a circle
Setting third.next = head connects the last node back to the first, making the list circular.Final Answer:
third.next = head; -> Option AQuick Check:
Last node points to head for circular list [OK]
- Setting next to null keeps list linear
- Pointing head.next to third breaks order
- Pointing second.next to head creates wrong cycle
Ready to prove it?
Pass the test and this pattern moves unknown
