📖 Scenario: Imagine you want to keep a list of your favorite fruits in order. You want to add fruits one by one and see the list grow. A singly linked list is a simple way to do this, where each fruit points to the next fruit.
🎯 Goal: You will build a singly linked list from scratch in TypeScript. You will create nodes, link them, and finally print the list to see all fruits in order.
📋 What You'll Learn
Create a Node class with a value and a next pointer
Create a LinkedList class with a head pointer
Add nodes to the linked list
Print the linked list values in order
💡 Why This Matters
🌍 Real World
Linked lists are used in many software systems to manage ordered data where insertion and deletion happen often, like music playlists or undo history.
💼 Career
Understanding linked lists helps in technical interviews and is a foundation for learning more complex data structures used in software development.
Progress0 / 4 steps
1
Create the Node class
Create a class called Node with a constructor that takes a value of type string and sets this.value to it. Also, set this.next to null.
DSA Typescript
Hint
Think of each node as a box holding a fruit name and a pointer to the next box.
2
Create the LinkedList class with head
Create a class called LinkedList with a property head initialized to null in the constructor.
DSA Typescript
Hint
The linked list starts empty, so head points to nothing at first.
3
Add a method to insert nodes at the end
Inside the LinkedList class, add a method called append that takes a value of type string. Create a new Node with this value. If head is null, set head to the new node. Otherwise, traverse the list to the last node and set its next to the new node.
DSA Typescript
Hint
Think of walking through the chain of fruits until you find the last one, then add the new fruit after it.
4
Print the linked list values
Create a method called printList inside the LinkedList class. It should start from head and collect all values in order, separated by -> . Print the final string. Then, create a linked list, append "Apple", "Banana", and "Cherry", and call printList().
DSA Typescript
Hint
Walk through each node, add its value to a string, and print the full chain.
Practice
(1/5)
1. What is the main purpose of a Singly Linked List in data structures?
easy
A. To store elements in a sequence where each element points to the next one
B. To store elements with keys and values for fast lookup
C. To store elements in a sorted array for quick access
D. To store elements in a tree-like hierarchy
Solution
Step 1: Understand the structure of a singly linked list
A singly linked list stores elements in nodes where each node points to the next node in the sequence.
Step 2: Compare with other data structures
Unlike key-value stores or trees, singly linked lists maintain a simple linear chain of elements.
Final Answer:
To store elements in a sequence where each element points to the next one -> Option A
Quick Check:
Singly Linked List = sequence with next pointers [OK]
Hint: Remember: singly linked list nodes point only forward [OK]
Common Mistakes:
Confusing linked lists with arrays
Thinking linked lists store key-value pairs
Assuming linked lists are hierarchical like trees
2. Which of the following is the correct TypeScript syntax to define a node class for a singly linked list?
Hint: Append adds at end; print shows sequence [OK]
Common Mistakes:
Assuming append adds at head instead of tail
Forgetting to add 'null' at the end
Printing values in reverse order
4. Identify the error in this TypeScript code snippet that tries to append a node to a singly linked list:
class Node {
value: number;
next: Node | null;
constructor(value: number) {
this.value = value;
this.next = null;
}
}
class LinkedList {
head: Node | null = null;
append(value: number) {
const newNode = new Node(value);
if (this.head === null) {
this.head = newNode;
} else {
let current = this.head;
while (current.next !== null) {
current = current.next;
}
current.next = newNode;
}
}
}
const list = new LinkedList();
list.append(5);
list.append(10);
console.log(list.head.next.value);
medium
A. Error: head is undefined
B. Error: Cannot read property 'value' of null
C. No error; output will be 10
D. Error: next property is missing in Node
Solution
Step 1: Check append logic
The append method correctly adds the first node as head and subsequent nodes at the end.
Step 2: Verify output of console.log
After appending 5 and 10, head.next points to the node with value 10, so accessing value is valid.
Final Answer:
No error; output will be 10 -> Option C
Quick Check:
Proper append and access = 10 [OK]
Hint: Check if head and next are properly assigned before access [OK]
Common Mistakes:
Forgetting to return after setting head in append
Accessing next.value when next is null
Not initializing next to null in Node
5. You want to create a method reverse() in your singly linked list class that reverses the list in place. Which approach correctly reverses the links between nodes?
hard
A. Create a new list and append nodes from the original list in reverse order
B. Iterate through the list, for each node set its next to the previous node, updating previous and current pointers
C. Swap the values of the first and last nodes repeatedly until the middle is reached
D. Remove all nodes and re-insert them at the head one by one
Solution
Step 1: Understand in-place reversal
Reversing links means changing each node's next pointer to point to the previous node, not creating new nodes.
Step 2: Identify correct method
Iterating with previous and current pointers and updating next links reverses the list efficiently in place.
Final Answer:
Iterate through the list, for each node set its next to the previous node, updating previous and current pointers -> Option B
Quick Check:
In-place reversal = update next pointers backward [OK]
Hint: Reverse by redirecting next pointers, not swapping values [OK]
Common Mistakes:
Swapping node values instead of links
Creating a new list instead of reversing in place
Removing and re-adding nodes unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.