Bird
Raised Fist0
DSA Javascriptprogramming

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

Choose your learning style10 modes available
Mental Model
A linear linked list ends with a node pointing to nothing, while a circular linked list loops back to the start, making a circle.
Analogy: Imagine a line of people holding hands (linear), versus a circle of people holding hands where the last person holds the first person's hand (circular).
Linear: 1 -> 2 -> 3 -> null
Circular: 1 -> 2 -> 3 -> (back to 1)
Dry Run Walkthrough
Input: list: 1 -> 2 -> 3, show difference between linear and circular
Goal: Understand how the last node points in linear vs circular linked list
Step 1: Create linear list nodes 1, 2, 3 with last node pointing to null
1 -> 2 -> 3 -> null
Why: Linear list ends with null to mark the end
Step 2: Create circular list nodes 1, 2, 3 with last node pointing back to first node
1 -> 2 -> 3 -> (back to 1)
Why: Circular list loops back to start to form a circle
Step 3: Traverse linear list until null is found
Traverse: 1 -> 2 -> 3 -> null (stop)
Why: Traversal ends at null in linear list
Step 4: Traverse circular list and stop after visiting all nodes once
Traverse: 1 -> 2 -> 3 -> 1 (stop to avoid infinite loop)
Why: Traversal must stop manually to avoid infinite loop in circular list
Result:
Linear: 1 -> 2 -> 3 -> null
Circular: 1 -> 2 -> 3 -> (back to 1)
Annotated Code
DSA Javascript
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

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

// Create circular linked list 1->2->3->(back to 1)
function createCircularList() {
  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) {
  let curr = head;
  let result = '';
  while (curr !== null) {
    result += curr.value + ' -> ';
    curr = curr.next;
  }
  result += 'null';
  console.log(result);
}

// Print circular list (stop after one full cycle)
function printCircularList(head) {
  let curr = head;
  let result = '';
  if (!head) {
    console.log('null');
    return;
  }
  do {
    result += curr.value + ' -> ';
    curr = curr.next;
  } while (curr !== head);
  result += '(back to ' + head.value + ')';
  console.log(result);
}

// Driver code
const linearHead = createLinearList();
const circularHead = createCircularList();

printLinearList(linearHead);
printCircularList(circularHead);
head.next.next.next = head; // last node points back to head
make last node point to first node to form circular list
while (curr !== null) {
traverse linear list until end marked by null
do { ... } while (curr !== head);
traverse circular list stopping after one full cycle to avoid infinite loop
OutputSuccess
1 -> 2 -> 3 -> null 1 -> 2 -> 3 -> (back to 1)
Complexity Analysis
Time: O(n) because we visit each node once during traversal
Space: O(n) because we store n nodes in memory
vs Alternative: Circular lists require careful traversal to avoid infinite loops, unlike linear lists which end naturally at null
Edge Cases
empty list (head is null)
print functions handle empty list by printing 'null' or nothing
DSA Javascript
if (!head) { console.log('null'); return; }
single node list
linear list ends at null, circular list points back to itself
DSA Javascript
head.next = head; // circular case
When to Use This Pattern
When you see a linked list where the last node points back to the first node, recognize it as a circular linked list; otherwise, it's linear.
Common Mistakes
Mistake: Trying to traverse a circular linked list without stopping condition causes infinite loop
Fix: Use a stopping condition like checking if current node is head again to stop traversal
Mistake: Assuming last node's next is always null in circular list
Fix: Remember last node points back to head in circular list
Summary
Linear linked list ends with a node pointing to null; circular linked list loops back to the first node.
Use circular linked lists when you want to cycle through elements repeatedly without end.
The key insight is that circular lists have no null end, so traversal must detect when it loops back to start.

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; in a linear linked list, the last node points to null.
B. In a linear linked list, nodes form a circle; in a circular linked list, nodes end with null.
C. Circular linked lists store data in a circle shape; linear linked lists store data in a straight line.
D. Circular linked lists use arrays internally; linear linked lists use pointers.

Solution

  1. Step 1: Understand the pointer of the last node in linear linked list

    In a linear linked list, the last node's next pointer is null, indicating the end of the list.
  2. Step 2: Understand the pointer of the last node in circular linked list

    In a circular linked list, the last node's next pointer points back to the first node, forming a circle.
  3. Final Answer:

    In a circular linked list, the last node points back to the first node; in a linear linked list, the last node points to null. -> Option A
  4. Quick Check:

    Last node pointer difference = In a circular linked list, the last node points back to the first node; in a linear linked list, the last node points to null. [OK]
Hint: Last node points null in linear, loops to head in circular [OK]
Common Mistakes:
  • Confusing the direction of pointers in circular vs linear
  • Thinking circular lists end with null
  • Assuming circular lists use arrays internally
2. Which of the following is the correct way to check if a linked list is circular in JavaScript?
easy
A. if (head.next === head) { /* circular list */ } else if (head.next === null) { /* linear list */ }
B. if (head === null) { /* empty list */ } else if (head.next === head) { /* circular list */ }
C. if (head.next === null) { /* linear list */ } else { /* circular list */ }
D. if (head.next === undefined) { /* circular list */ }

Solution

  1. Step 1: Check the next pointer of head node

    If head.next points to head itself, it means the list is circular with one node.
  2. Step 2: Check if next pointer is null for linear list

    If head.next is null, it means the list ends there, so it is linear.
  3. Final Answer:

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

    Check next pointer for circular or null for linear [OK]
Hint: Check if next points to head for circular, null for linear [OK]
Common Mistakes:
  • Checking undefined instead of null
  • Assuming head.next null means circular
  • Not handling empty list (head null) case
3. Consider the following JavaScript code for a circular linked list with three nodes. What will be the output when traversing and printing the list once starting from head?
class Node {
  constructor(data) {
    this.data = data;
    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; // circular link

let current = head;
let output = '';
do {
  output += current.data + ' -> ';
  current = current.next;
} while (current !== head);
output += 'null';
console.log(output);
medium
A. Infinite loop, no output
B. 1 -> 2 -> 3 -> 1 -> 2 -> 3 -> null
C. 1 -> 2 -> 3 -> 1 -> null
D. 1 -> 2 -> 3 -> null

Solution

  1. Step 1: Understand the circular linked list structure

    The last node points back to the head, creating a loop: 1 -> 2 -> 3 -> 1 ...
  2. Step 2: Trace the traversal loop once

    The do-while loop prints nodes starting at head and stops when it reaches head again after one full cycle, printing "1 -> 2 -> 3 -> null".
  3. Final Answer:

    1 -> 2 -> 3 -> null -> Option D
  4. Quick Check:

    Traverse once until back to head = 1 -> 2 -> 3 -> null [OK]
Hint: Traverse until current equals head again to print once [OK]
Common Mistakes:
  • Thinking loop prints infinitely
  • Appending extra nodes after stopping
  • Confusing do-while with while loop
4. The following code attempts to detect if a linked list is circular. What is the bug?
function isCircular(head) {
  let current = head;
  while (current !== null) {
    if (current.next === head) {
      return true;
    }
    current = current.next;
  }
  return false;
}
medium
A. It does not handle empty list (head null) case.
B. It incorrectly returns true for linear lists.
C. It may cause infinite loop if list has a cycle not pointing to head.
D. It uses strict equality instead of loose equality.

Solution

  1. Step 1: Analyze the loop condition and pointer movement

    The loop moves through nodes until current is null, checking if current.next equals head.
  2. Step 2: Identify the problem with cycles not pointing to head

    If the list has a cycle that does not point back to head, the loop never reaches null and never finds current.next === head, causing an infinite loop.
  3. Final Answer:

    It may cause infinite loop if list has a cycle not pointing to head. -> Option C
  4. Quick Check:

    Cycle detection fails if cycle not at head [OK]
Hint: Cycle may not point to head, causing infinite loop [OK]
Common Mistakes:
  • Assuming all cycles point to head
  • Not checking for null head before loop
  • Confusing strict and loose equality
5. You have a linked list where the last node points back to the third node (not the head). Which type of linked list is this, and what is the key difference from a linear linked list?
hard
A. It is a linear linked list; the key difference is the last node points to the third node.
B. It is a circular linked list; the key difference is the last node points to a node inside the list, not null.
C. It is a doubly linked list; the key difference is nodes have two pointers.
D. It is a circular linked list; the key difference is the last node points to the head only.

Solution

  1. Step 1: Identify the type of linked list by last node pointer

    The last node points back to the third node, creating a loop inside the list, which defines a circular linked list.
  2. Step 2: Compare with linear linked list ending

    In a linear linked list, the last node points to null, marking the end. Here, the last node points inside the list, so it is circular.
  3. Final Answer:

    It is a circular linked list; the key difference is the last node points to a node inside the list, not null. -> Option B
  4. Quick Check:

    Last node points inside list = circular list [OK]
Hint: Last node points inside list means circular, not linear [OK]
Common Mistakes:
  • Thinking only head pointer defines circular list
  • Confusing doubly linked list with circular list
  • Assuming last node must point to head for circular
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