📖 Scenario: You are building a simple linked list to keep track of tasks in the order they arrive. New tasks should be added at the beginning of the list so they get handled first.
🎯 Goal: Create a singly linked list and write code to insert new nodes at the beginning (head) of the list. Then print the list to see the order of tasks.
📋 What You'll Learn
Create a Node class with value and next properties
Create a LinkedList class with a head property
Write a method insertAtBeginning(value: string) to add a new node at the start
Write a method printList() to print all node values in order
💡 Why This Matters
🌍 Real World
Linked lists are used in real-world apps like task schedulers, undo features, and navigation history where order and quick insertions matter.
💼 Career
Understanding linked lists is fundamental for software engineering roles, especially those involving data structure optimization and system design.
Progress0 / 4 steps
1
Create Node and LinkedList classes
Create a Node class with a value property of type string and a next property of type Node | null. Then create a LinkedList class with a head property initialized to null.
DSA Typescript
Hint
Define the Node class with value and next. Then define LinkedList with head set to null.
2
Add insertAtBeginning method
Inside the LinkedList class, add a method called insertAtBeginning(value: string). This method should create a new Node with the given value, set its next to the current head, and then update head to this new node.
DSA Typescript
Hint
Create a new node, point its next to current head, then update head to new node.
3
Add printList method
Inside the LinkedList class, add a method called printList(). This method should start from head and loop through each node, collecting their value in an array. Then join the array with ' -> ' and print the result.
DSA Typescript
Hint
Use a loop to collect all node values, then print them joined by ' -> '.
4
Insert nodes and print the list
Create a new LinkedList instance called list. Use insertAtBeginning to add these values in order: 'Task 3', 'Task 2', 'Task 1'. Then call list.printList() to print the list.
DSA Typescript
Hint
Create the list, insert the tasks in the given order, then print the list.
Practice
(1/5)
1. What does the "Insert at Beginning" operation do in a singly linked list?
easy
A. Adds a new node at the start of the list
B. Adds a new node at the end of the list
C. Deletes the first node of the list
D. Deletes the last node of the list
Solution
Step 1: Understand the operation
Inserting at the beginning means adding a new node before the current first node.
Step 2: Effect on the list
The new node becomes the head, pushing the old head to second position.
Final Answer:
Adds a new node at the start of the list -> Option A
Quick Check:
Insert at beginning = Add new head [OK]
Hint: Insert at beginning means new head node [OK]
Common Mistakes:
Confusing insert at beginning with insert at end
Thinking it deletes nodes
Assuming it changes the tail node
2. Which of the following is the correct TypeScript syntax to insert a new node at the beginning of a singly linked list?
easy
A. newNode = head; head.next = newNode;
B. head = newNode.next; newNode = head;
C. head.next = newNode; newNode = head;
D. newNode.next = head; head = newNode;
Solution
Step 1: Link new node to current head
Set newNode.next to point to current head node.
Step 2: Update head to new node
Assign head to newNode so it becomes the first node.
Final Answer:
newNode.next = head; head = newNode; -> Option D
Quick Check:
Link new node to head, then update head [OK]
Hint: Set newNode.next to head, then head = newNode [OK]
Common Mistakes:
Assigning head before linking newNode.next
Swapping assignments order
Using head.next instead of newNode.next
3. What will be the linked list after executing the following TypeScript code?
class Node {
value: number;
next: Node | null = null;
constructor(value: number) { this.value = value; }
}
let head: Node | null = new Node(10);
let newNode = new Node(5);
newNode.next = head;
head = newNode;
medium
A. 10 -> null
B. 10 -> 5 -> null
C. 5 -> 10 -> null
D. 5 -> null
Solution
Step 1: Initial list setup
head points to node with value 10, next is null.
Step 2: Insert newNode at beginning
newNode (5) points to head (10), then head updated to newNode.
Final Answer:
5 -> 10 -> null -> Option C
Quick Check:
New node 5 becomes head, points to 10 [OK]
Hint: New node points to old head, then becomes new head [OK]
Common Mistakes:
Confusing order of nodes after insertion
Forgetting to update head
Assuming newNode.next is null
4. Identify the error in this TypeScript code that tries to insert a node at the beginning of a linked list:
class Node {
value: number;
next: Node | null = null;
constructor(value: number) { this.value = value; }
}
let head: Node | null = null;
let newNode = new Node(7);
head.next = newNode;
head = newNode;
medium
A. head is null, so head.next causes an error
B. newNode is not linked to head
C. head should be assigned before newNode.next
D. Missing constructor call for newNode
Solution
Step 1: Check initial head value
head is null, so accessing head.next is invalid and causes runtime error.
Step 2: Correct insertion approach
Assign newNode.next = head (which is null), then head = newNode.
Final Answer:
head is null, so head.next causes an error -> Option A
Quick Check:
Cannot access property on null head [OK]
Hint: Check if head is null before accessing head.next [OK]
Common Mistakes:
Trying to access next on null head
Not linking newNode.next to head
Assigning head after using head.next
5. Given a singly linked list with nodes 20 -> 30 -> 40 -> null, what will be the list after inserting 10 at the beginning using head insert method?
hard
A. 20 -> 30 -> 40 -> 10 -> null
B. 10 -> 20 -> 30 -> 40 -> null
C. 30 -> 40 -> 10 -> 20 -> null
D. 10 -> 30 -> 40 -> 20 -> null
Solution
Step 1: Understand initial list
List is 20 -> 30 -> 40 -> null, head points to 20.
Step 2: Insert 10 at beginning
Create new node 10, set newNode.next = head (20), then head = newNode.
Final Answer:
10 -> 20 -> 30 -> 40 -> null -> Option B
Quick Check:
New node 10 becomes head, old list follows [OK]
Hint: New node points to old head, becomes new head [OK]
Common Mistakes:
Appending new node at end instead of beginning
Mixing node order after insertion
Not updating head pointer
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.