Creating a Singly Linked List from Scratch in DSA Typescript - Complexity Walkthrough
When we build a singly linked list from scratch, we want to know how the time it takes grows as we add more items.
We ask: How does adding nodes affect the total work done?
Analyze the time complexity of the following code snippet.
class Node {
value: number;
next: Node | null = null;
constructor(value: number) {
this.value = value;
}
}
class SinglyLinkedList {
head: Node | null = null;
append(value: number) {
const newNode = new Node(value);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
}
This code creates a singly linked list and adds new nodes to the end.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The
whileloop that moves through nodes to find the list end. - How many times: It runs once for each node already in the list before adding the new one.
As the list grows, adding a new node takes longer because we must walk through all existing nodes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps to add the 10th node |
| 100 | About 100 steps to add the 100th node |
| 1000 | About 1000 steps to add the 1000th node |
Pattern observation: The work grows linearly with the number of nodes already in the list.
Time Complexity: O(n)
This means adding a new node takes time proportional to the current list size.
[X] Wrong: "Adding a node always takes the same time, no matter the list size."
[OK] Correct: Because we must walk through all nodes to find the end, adding takes longer as the list grows.
Understanding this helps you explain how linked lists work and why some operations are slower than others, a key skill in coding interviews.
"What if we kept a pointer to the last node? How would the time complexity of adding a node change?"
Practice
Singly Linked List in data structures?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 AQuick Check:
Singly Linked List = sequence with next pointers [OK]
- Confusing linked lists with arrays
- Thinking linked lists store key-value pairs
- Assuming linked lists are hierarchical like trees
Solution
Step 1: Check the type of the next pointer
The next pointer should be either another Node or null if it is the last node.Step 2: Verify constructor initialization
The constructor sets the value and initializes next to null, which is correct for a new node.Final Answer:
class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } -> Option AQuick Check:
next can be Node or null = class Node { value: number; next: Node | null; constructor(value: number) { this.value = value; this.next = null; } } [OK]
- Forgetting to allow next to be null
- Setting next to undefined instead of null
- Using any type for next which loses type safety
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) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next) {
current = current.next;
}
current.next = newNode;
}
print() {
let current = this.head;
let result = '';
while (current) {
result += current.value + ' -> ';
current = current.next;
}
result += 'null';
console.log(result);
}
}
const list = new LinkedList();
list.append(10);
list.append(20);
list.append(30);
list.print();Solution
Step 1: Append nodes in order
Nodes with values 10, 20, and 30 are appended in that order, so the list is 10 -> 20 -> 30.Step 2: Print the list
The print method traverses from head to end, concatenating values with ' -> ' and ending with 'null'.Final Answer:
10 -> 20 -> 30 -> null -> Option DQuick Check:
Appending preserves order = 10 -> 20 -> 30 -> null [OK]
- Assuming append adds at head instead of tail
- Forgetting to add 'null' at the end
- Printing values in reverse order
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);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 CQuick Check:
Proper append and access = 10 [OK]
- Forgetting to return after setting head in append
- Accessing next.value when next is null
- Not initializing next to null in Node
reverse() in your singly linked list class that reverses the list in place. Which approach correctly reverses the links between nodes?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 BQuick Check:
In-place reversal = update next pointers backward [OK]
- Swapping node values instead of links
- Creating a new list instead of reversing in place
- Removing and re-adding nodes unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
