Bird
Raised Fist0
DSA Typescriptprogramming

Circular vs Linear Linked List Key Difference in DSA Typescript - Trade-offs & Analysis

Choose your learning style10 modes available
Mental Model
A linear linked list ends with a null pointer, while a circular linked list loops back to the start, forming a circle.
Analogy: Imagine a line of people holding hands (linear list) versus a circle of people holding hands where the last person holds the first person's hand (circular list).
Linear List:
1 -> 2 -> 3 -> null

Circular List:
1 -> 2 -> 3 ->
↑               ↓
← ← ← ← ← ← ← ←
Dry Run Walkthrough
Input: Two lists: Linear list 1->2->3->null and Circular list 1->2->3->(back to 1)
Goal: Show how the last node points differently in linear vs circular linked lists
Step 1: Create linear list nodes 1, 2, 3 with next pointers
1 -> 2 -> 3 -> null
Why: To build a simple linear linked list ending with null
Step 2: Create circular list nodes 1, 2, 3 with next pointers
1 -> 2 -> 3 ->
↑               ↓
← ← ← ← ← ← ← ←
Why: To build a circular linked list where last node points back to first
Step 3: Traverse linear list until next is null
Traverse: 1 -> 2 -> 3 -> null
Why: Traversal stops at null, end of list
Step 4: Traverse circular list for 5 steps to show looping
Traverse: 1 -> 2 -> 3 -> 1 -> 2 -> ...
Why: Traversal loops back to start, no null end
Result:
Linear List: 1 -> 2 -> 3 -> null
Circular List: 1 -> 2 -> 3 -> 1 -> 2 -> ...
Annotated Code
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

// Build linear linked list 1->2->3->null
function buildLinearList(): Node {
  const head = new Node(1);
  head.next = new Node(2);
  head.next.next = new Node(3);
  return head;
}

// Build circular linked list 1->2->3->(back to 1)
function buildCircularList(): Node {
  const head = new Node(1);
  head.next = new Node(2);
  head.next.next = new Node(3);
  head.next.next.next = head; // last node points back to head
  return head;
}

// Print linear list
function printLinearList(head: Node | null): string {
  let curr = head;
  let result = '';
  while (curr !== null) {
    result += curr.value + ' -> ';
    curr = curr.next;
  }
  result += 'null';
  return result;
}

// Print circular list for n steps to avoid infinite loop
function printCircularList(head: Node | null, steps: number): string {
  let curr = head;
  let result = '';
  let count = 0;
  while (curr !== null && count < steps) {
    result += curr.value + ' -> ';
    curr = curr.next;
    count++;
  }
  result += '...';
  return result;
}

// Driver code
const linearHead = buildLinearList();
const circularHead = buildCircularList();
console.log('Linear List:', printLinearList(linearHead));
console.log('Circular List:', printCircularList(circularHead, 5));
head.next.next.next = head; // last node points back to head
Create circular link by pointing last node's next to head
while (curr !== null) { result += curr.value + ' -> '; curr = curr.next; }
Traverse linear list until null to print nodes
while (curr !== null && count < steps) { result += curr.value + ' -> '; curr = curr.next; count++; }
Traverse circular list limited by steps to avoid infinite loop
OutputSuccess
Linear List: 1 -> 2 -> 3 -> null Circular List: 1 -> 2 -> 3 -> 1 -> 2 -> ...
Complexity Analysis
Time: O(n) because we visit each node once during traversal
Space: O(1) because we use only a few pointers and no extra data structures
vs Alternative: Linear list traversal ends at null, circular list requires step limit to avoid infinite loop
Edge Cases
Empty list (head is null)
Print functions handle null head and print 'null' or '...'
DSA Typescript
while (curr !== null) { ... } in printLinearList and printCircularList
Single node circular list (node points to itself)
Circular print loops on single node, limited by steps to avoid infinite loop
DSA Typescript
while (curr !== null && count < steps) { ... } in printCircularList
When to Use This Pattern
When you see a linked list where the last node points back to the first, recognize it as a circular linked list, useful for problems needing continuous looping.
Common Mistakes
Mistake: Trying to traverse a circular linked list without a step limit causes infinite loops.
Fix: Always limit traversal steps or detect cycles to avoid infinite loops.
Summary
Linear linked lists end with a null pointer; circular linked lists loop back to the start node.
Use circular linked lists when you need to cycle through elements repeatedly without stopping.
The key difference is whether the last node points to null (linear) or back to the head (circular).

Practice

(1/5)
1. What is the main difference between a circular linked list and a linear linked list?
easy
A. In a circular linked list, the last node points back to the first node, forming a circle.
B. In a linear linked list, the last node points back to the first node, forming a circle.
C. A circular linked list has no nodes.
D. A linear linked list always has only one node.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    In a circular linked list, the last node points back to the first node, forming a circle. -> Option A
  4. Quick Check:

    Circular list loops back = In a circular linked list, the last node points back to the first node, forming a circle. [OK]
Hint: Circular loops back to start; linear ends with null [OK]
Common Mistakes:
  • Confusing which list loops back
  • Thinking linear list forms a circle
  • Assuming circular list has no end node
2. Which of the following is the correct way to check if a single-node linked list is circular in TypeScript?
easy
A. if (head.next === 0) { /* linear list */ }
B. if (head.next === null) { /* linear list */ }
C. if (head.next === undefined) { /* circular list */ }
D. if (head.next === head) { /* circular list */ }

Solution

  1. 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.
  2. Step 2: Check the condition for circular list

    If head.next equals head, it means the list loops back to the start, confirming circularity.
  3. Final Answer:

    if (head.next === head) { /* circular list */ } -> Option D
  4. Quick Check:

    Next points to head means circular [OK]
Hint: Check if next points to head for circular list [OK]
Common Mistakes:
  • Checking for null instead of head
  • Using undefined instead of null
  • Confusing zero with null pointer
3. Consider this TypeScript code for a circular linked list:
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?
medium
A. 1 2 3 null null
B. 1 2 3 1 2
C. 1 2 3 4 5
D. 1 2 3 3 3

Solution

  1. Step 1: Understand the circular linked list structure

    The last node (third) points back to head, so the list loops: 1 -> 2 -> 3 -> 1 -> ...
  2. 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.
  3. Final Answer:

    1 2 3 1 2 -> Option B
  4. Quick Check:

    Circular list repeats nodes in order [OK]
Hint: Circular list repeats nodes; count controls output length [OK]
Common Mistakes:
  • Assuming list ends after 3 nodes
  • Printing null after last node
  • Confusing node values after loop
4. The following TypeScript code attempts to detect if a linked list is circular but has a bug:
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?
medium
A. It incorrectly returns true for linear lists.
B. It does not handle empty lists (head = null).
C. It may enter an infinite loop if the list is circular but not pointing directly to head.
D. It uses 'current.next!' which causes a syntax error.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    It may enter an infinite loop if the list is circular but not pointing directly to head. -> Option C
  4. Quick Check:

    Loop may never end if cycle not at head [OK]
Hint: Check for cycles anywhere, not just pointing to head [OK]
Common Mistakes:
  • Assuming only last node points to head
  • Ignoring infinite loop possibility
  • Confusing syntax error with logic error
5. You want to convert a linear linked list into a circular linked list in TypeScript. Given the code below, which line correctly completes the conversion?
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;
medium
A. third.next = head;
B. second.next = head;
C. head.next = third;
D. third.next = null;

Solution

  1. Step 1: Understand the current list structure

    The list is linear: head -> second -> third -> null.
  2. 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.
  3. Final Answer:

    third.next = head; -> Option A
  4. Quick Check:

    Last node points to head for circular list [OK]
Hint: Last node's next must point to head for circular list [OK]
Common Mistakes:
  • Setting next to null keeps list linear
  • Pointing head.next to third breaks order
  • Pointing second.next to head creates wrong cycle
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard