Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Circular vs Linear Linked List Key Difference in DSA Typescript - Compare & Choose

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Key difference between Circular and Linear Linked Lists

Which statement correctly describes the main difference between a circular linked list and a linear linked list?

ABoth circular and linear linked lists have their last node pointing to null, but circular linked lists have an extra pointer to the middle node.
BIn a circular linked list, the last node points back to the first node, forming a loop; in a linear linked list, the last node points to null.
CCircular linked lists store data in a circle shape physically in memory, while linear linked lists store data in a straight line.
DIn a linear linked list, the last node points back to the first node, forming a loop; in a circular linked list, the last node points to null.
Attempts:
2 left
💡 Hint

Think about how the last node connects in each list type.

❓ Predict Output
intermediate
2:00remaining
Output of traversing a circular linked list

Given the following TypeScript code for a circular linked list, what will be the output?

DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

const node1 = new Node(1);
const node2 = new Node(2);
const node3 = new Node(3);

node1.next = node2;
node2.next = node3;
node3.next = node1; // circular link

let current: Node | null = node1;
let count = 0;
const output: number[] = [];
while (current && count < 5) { // limit to 5 to avoid infinite loop
  output.push(current.value);
  current = current.next;
  count++;
}
console.log(output.join(' -> ') + ' -> ...');
A1 -> 2 -> null -> ... (runtime error)
B1 -> 2 -> 3 -> null
C1 -> 2 -> 3 -> 3 -> 3 -> ...
D1 -> 2 -> 3 -> 1 -> 2 -> ...
Attempts:
2 left
💡 Hint

Notice the loop and the count limit in the while loop.

🔧 Debug
advanced
1:30remaining
Identify the bug in linear linked list traversal

What error will occur when running this TypeScript code that tries to traverse a linear linked list?

DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

const node1 = new Node(1);
const node2 = new Node(2);
const node3 = new Node(3);

node1.next = node2;
node2.next = node3;
node3.next = null;

let current: Node = node1;
while (current.next !== null) {
  console.log(current.value);
  current = current.next;
}
console.log(current.value);
AInfinite loop because current.next never becomes null
BRuntime error: Cannot read property 'next' of null
CNo error; prints 1 2 3 correctly
DSyntax error due to missing semicolon
Attempts:
2 left
💡 Hint

Check the loop condition and what is printed inside and after the loop.

❓ Predict Output
advanced
2:00remaining
Output after inserting node in circular linked list

What is the output after inserting a new node with value 4 after node 2 in this circular linked list?

DSA Typescript
class Node {
  value: number;
  next: Node | null;
  constructor(value: number) {
    this.value = value;
    this.next = null;
  }
}

const node1 = new Node(1);
const node2 = new Node(2);
const node3 = new Node(3);

node1.next = node2;
node2.next = node3;
node3.next = node1; // circular

const newNode = new Node(4);
newNode.next = node2.next;
node2.next = newNode;

let current: Node | null = node1;
let count = 0;
const output: number[] = [];
while (current && count < 7) {
  output.push(current.value);
  current = current.next;
  count++;
}
console.log(output.join(' -> ') + ' -> ...');
A1 -> 2 -> 4 -> 3 -> 1 -> 2 -> 4 -> ...
B1 -> 2 -> 3 -> 4 -> 1 -> 2 -> 3 -> ...
C1 -> 4 -> 2 -> 3 -> 1 -> 4 -> 2 -> ...
D1 -> 2 -> 3 -> 1 -> 2 -> 3 -> 1 -> ...
Attempts:
2 left
💡 Hint

Focus on where the new node is inserted and how the links change.

🧠 Conceptual
expert
1:30remaining
Memory usage difference between circular and linear linked lists

Which statement best explains the memory usage difference between circular and linear linked lists?

ABoth use the same amount of memory per node; the difference is only in how pointers are connected, not in memory size.
BCircular linked lists use slightly more memory because the last node stores a pointer back to the first node, while linear linked lists store null.
CLinear linked lists use more memory because they require an extra pointer to mark the end, while circular linked lists do not.
DCircular linked lists use less memory because they reuse the first node's memory for the last node's pointer.
Attempts:
2 left
💡 Hint

Consider the size of each node and what pointers they hold.

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