Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Push Using Linked List Node in DSA Typescript - Practice Problems & Challenges

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!
❓ Predict Output
intermediate
2:00remaining
What is the output after pushing nodes in this linked list?

Consider a singly linked list initially empty. We push nodes with values 10, 20, then 30 in that order. What is the printed state of the linked list?

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

class LinkedList {
  head: Node | null = null;

  push(value: number) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
    } else {
      let current = this.head;
      while (current.next) {
        current = current.next;
      }
      current.next = newNode;
    }
  }

  printList(): string {
    let result = '';
    let current = this.head;
    while (current) {
      result += current.value + ' -> ';
      current = current.next;
    }
    return result + 'null';
  }
}

const list = new LinkedList();
list.push(10);
list.push(20);
list.push(30);
console.log(list.printList());
A10 -> 20 -> 30 -> null
Bnull
C10 -> 30 -> 20 -> null
D30 -> 20 -> 10 -> null
Attempts:
2 left
💡 Hint

Remember, pushing adds nodes at the end of the list.

❓ Predict Output
intermediate
2:00remaining
What is the output after pushing nodes with duplicate values?

We push nodes with values 5, 5, 10 into an empty linked list. What is the printed state?

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

class LinkedList {
  head: Node | null = null;

  push(value: number) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
    } else {
      let current = this.head;
      while (current.next) {
        current = current.next;
      }
      current.next = newNode;
    }
  }

  printList(): string {
    let result = '';
    let current = this.head;
    while (current) {
      result += current.value + ' -> ';
      current = current.next;
    }
    return result + 'null';
  }
}

const list = new LinkedList();
list.push(5);
list.push(5);
list.push(10);
console.log(list.printList());
A10 -> 5 -> 5 -> null
B5 -> 5 -> 10 -> null
C5 -> 10 -> 5 -> null
D5 -> null
Attempts:
2 left
💡 Hint

Duplicates are allowed and pushed at the end.

🔧 Debug
advanced
2:30remaining
Why does this push method cause an infinite loop?

Examine the push method below. Why does it cause an infinite loop when pushing nodes?

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

class LinkedList {
  head: Node | null = null;

  push(value: number) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
    } else {
      let current = this.head;
      while (current.next !== null) {
        current = current.next;
      }
      current.next = current; // bug here
    }
  }
}
AIt uses wrong variable name for newNode.
BIt forgets to update current to current.next inside the loop.
CIt never initializes the head node.
DIt assigns current.next to current itself, creating a cycle causing infinite loop.
Attempts:
2 left
💡 Hint

Check what current.next points to after assignment.

🧠 Conceptual
advanced
1:30remaining
What is the time complexity of pushing a node at the end of a singly linked list without tail pointer?

Consider a singly linked list with no tail pointer. What is the time complexity of the push operation that adds a node at the end?

AO(n) linear time
BO(n^2) quadratic time
CO(1) constant time
DO(log n) logarithmic time
Attempts:
2 left
💡 Hint

Think about how many nodes you must traverse to reach the end.

❓ Predict Output
expert
3:00remaining
What is the output after pushing nodes and then reversing the list?

Given the linked list after pushing 1, 2, 3, what is the printed state after reversing the list?

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

class LinkedList {
  head: Node | null = null;

  push(value: number) {
    const newNode = new Node(value);
    if (!this.head) {
      this.head = newNode;
    } else {
      let current = this.head;
      while (current.next) {
        current = current.next;
      }
      current.next = newNode;
    }
  }

  reverse() {
    let prev: Node | null = null;
    let current = this.head;
    while (current) {
      const nextNode = current.next;
      current.next = prev;
      prev = current;
      current = nextNode;
    }
    this.head = prev;
  }

  printList(): string {
    let result = '';
    let current = this.head;
    while (current) {
      result += current.value + ' -> ';
      current = current.next;
    }
    return result + 'null';
  }
}

const list = new LinkedList();
list.push(1);
list.push(2);
list.push(3);
list.reverse();
console.log(list.printList());
Anull
B1 -> 2 -> 3 -> null
C3 -> 2 -> 1 -> null
D1 -> 3 -> 2 -> null
Attempts:
2 left
💡 Hint

Reversing flips the order of nodes.

Practice

(1/5)
1. What does the push operation do in a linked list when using nodes?
easy
A. Adds a new node at the beginning of the list
B. Removes the last node from the list
C. Searches for a node with a specific value
D. Deletes the first node of the list

Solution

  1. Step 1: Understand push operation in linked list

    The push operation adds a new node to the start (head) of the linked list.
  2. Step 2: Effect of push on linked list

    The new node becomes the new head, pointing to the previous first node.
  3. Final Answer:

    Adds a new node at the beginning of the list -> Option A
  4. Quick Check:

    Push means add at head = D [OK]
Hint: Push always adds node at the start of the list [OK]
Common Mistakes:
  • Confusing push with pop (removal)
  • Thinking push adds at the end
  • Mixing push with search or delete
2. Which of the following is the correct TypeScript syntax to create a new node and push it to the front of a singly linked list?
easy
A. newNode = new Node(value); head = newNode.next;
B. const newNode = Node(value); head.next = newNode; head = newNode;
C. const newNode = new Node(value); newNode.next = head; head = newNode;
D. head = new Node(value); head.next = null;

Solution

  1. Step 1: Create a new node with given value

    Use new Node(value) to create a node instance.
  2. Step 2: Link new node to current head and update head

    Set newNode.next = head and then head = newNode to push at front.
  3. Final Answer:

    const newNode = new Node(value); newNode.next = head; head = newNode; -> Option C
  4. Quick Check:

    New node points to old head, head updated = A [OK]
Hint: New node next points to head, then head updated [OK]
Common Mistakes:
  • Forgetting to link new node's next to old head
  • Assigning head.next instead of newNode.next
  • Not updating head to new node
3. What will be the linked list after executing this TypeScript code?
class Node {
  value: number;
  next: Node | null = null;
  constructor(value: number) { this.value = value; }
}

let head: Node | null = null;

function push(value: number) {
  const newNode = new Node(value);
  newNode.next = head;
  head = newNode;
}

push(10);
push(20);
push(30);
medium
A. null -> 10 -> 20 -> 30
B. 10 -> 20 -> 30 -> null
C. 20 -> 30 -> 10 -> null
D. 30 -> 20 -> 10 -> null

Solution

  1. Step 1: Push 10 to empty list

    Head becomes node with value 10, next is null.
  2. Step 2: Push 20 and then 30

    Each new node points to previous head, so order is 30 -> 20 -> 10 -> null.
  3. Final Answer:

    30 -> 20 -> 10 -> null -> Option D
  4. Quick Check:

    Last pushed is head = A [OK]
Hint: Last pushed node is always new head [OK]
Common Mistakes:
  • Assuming push adds at tail
  • Reading list in wrong order
  • Confusing next pointers
4. Identify the error in this push function for a singly linked list:
function push(value: number) {
  const newNode = new Node(value);
  head.next = newNode;
  head = newNode;
}
medium
A. head might be null causing runtime error
B. head.next = newNode is missing
C. head is assigned before linking newNode
D. Node constructor is called incorrectly

Solution

  1. Step 1: Check head usage before assignment

    The code uses head.next without checking if head is null.
  2. Step 2: Consequence of head being null

    If list is empty (head null), accessing head.next causes runtime error.
  3. Final Answer:

    head might be null causing runtime error -> Option A
  4. Quick Check:

    Accessing head.next without null check = B [OK]
Hint: Always check if head is null before accessing head.next [OK]
Common Mistakes:
  • Not checking for empty list before push
  • Forgetting to set newNode.next
  • Assigning head incorrectly
5. Given a linked list with nodes containing values [5, 15, 25] (5 is head), what will be the list after pushing 35 and then 45 using the push function?
Also, explain how the head changes after each push.
hard
A. 35 -> 45 -> 5 -> 15 -> 25 -> null; head moves to 35 then 45
B. 45 -> 35 -> 5 -> 15 -> 25 -> null; head moves to newest node each push
C. 5 -> 15 -> 25 -> 35 -> 45 -> null; head stays at 5
D. null; list is cleared after push

Solution

  1. Step 1: Initial list and push 35

    Original head is 5. Push 35 creates new node 35 pointing to 5. Head updates to 35.
  2. Step 2: Push 45 and update head

    Push 45 creates new node 45 pointing to 35. Head updates to 45. Final list: 45 -> 35 -> 5 -> 15 -> 25 -> null.
  3. Final Answer:

    45 -> 35 -> 5 -> 15 -> 25 -> null; head moves to newest node each push -> Option B
  4. Quick Check:

    Push adds at front, head updates = C [OK]
Hint: Head always points to last pushed node [OK]
Common Mistakes:
  • Assuming push adds at tail
  • Not updating head after push
  • Confusing node order after pushes
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