Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Circular vs Linear Linked List Key Difference in DSA Typescript - Visual Comparison

Choose your learning style10 modes available
Concept Flow - Circular vs Linear Linked List Key Difference
Start with head node
↓
Traverse nodes one by one
↓
Check if next node is null?
Yes→End of Linear List
No↓
Check if next node is head?
Yes→End of Circular List
No↓
Move to next node
↩Back to Check
Traverse nodes from head checking if next is null (linear) or points back to head (circular) to find list end.
Execution Sample
DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}
Defines a simple node class for linked lists with a value and next pointer.
Execution Table
StepOperationNodes in ListPointer ChangesVisual State
1Create nodes 1, 2, 31 -> 2 -> 3 -> nullhead points to node 11 -> 2 -> 3 -> null
2Linear list traversal from head1 -> 2 -> 3 -> nullcurrent moves from 1 to 21 -> 2 -> 3 -> null
3Linear list traversal continues1 -> 2 -> 3 -> nullcurrent moves from 2 to 31 -> 2 -> 3 -> null
4Check next of node 31 -> 2 -> 3 -> nullcurrent.next is null, end reached1 -> 2 -> 3 -> null
5Make list circular by linking node 3 to head1 -> 2 -> 3 -> (back to 1)node 3.next points to head (node 1)1 -> 2 -> 3 -> (back to 1)
6Circular list traversal from head1 -> 2 -> 3 -> (back to 1)current moves from 1 to 21 -> 2 -> 3 -> (back to 1)
7Circular list traversal continues1 -> 2 -> 3 -> (back to 1)current moves from 2 to 31 -> 2 -> 3 -> (back to 1)
8Check next of node 31 -> 2 -> 3 -> (back to 1)current.next is head, end reached1 -> 2 -> 3 -> (back to 1)
💡 Traversal stops when next is null (linear) or next is head (circular).
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6After Step 7After Step 8
headpoints to node 1points to node 1points to node 1points to node 1points to node 1points to node 1points to node 1
currentnullnode 2node 3node 3node 2node 3node 3
node3.nextnullnullnullnullpoints to node 1points to node 1points to node 1
Key Moments - 3 Insights
Why does traversal stop when current.next is null in linear list?
Because in linear lists, the last node's next is null, indicating the end. See execution_table step 4 where current.next is null and traversal ends.
Why does traversal stop when current.next points back to head in circular list?
Because in circular lists, the last node links back to head, so reaching head again means full circle. See execution_table step 8 where current.next is head and traversal ends.
What changes in the list structure to make it circular?
Linking the last node's next pointer to head instead of null makes the list circular. See execution_table step 5 where node 3.next points to head.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the value of current.next?
Anode 1 (head)
Bnode 2
Cnull
Dnode 3
💡 Hint
Check the 'Pointer Changes' and 'Visual State' columns at step 4.
At which step does the list become circular?
AStep 5
BStep 3
CStep 7
DStep 8
💡 Hint
Look for the step where node 3.next points to head in the 'Pointer Changes' column.
If node 3.next was not linked to head, what would happen during traversal at step 8?
ATraversal would skip node 3
BTraversal would stop because current.next is null
CTraversal would continue infinitely
DTraversal would restart from node 2
💡 Hint
Refer to the difference between linear and circular list traversal in the execution_table steps 4 and 8.
Concept Snapshot
Circular vs Linear Linked List Key Difference:
- Linear list ends when next is null.
- Circular list ends when next points back to head.
- Circular list forms a loop; linear does not.
- Traversal stops accordingly.
- Last node's next pointer defines the type.
Full Transcript
This visual execution shows the key difference between circular and linear linked lists. We start by creating three nodes linked linearly with the last node's next as null. Traversing this linear list stops when current.next is null. Then we change the last node's next to point back to the head, making the list circular. Traversal now stops when current.next points back to head, completing a full circle. The pointer changes and visual states at each step highlight how the list structure changes and how traversal conditions differ.

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