Challenge - 5 Problems
Singly Linked List Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of inserting nodes at the head
What is the printed state of the singly linked list after inserting nodes 10, 20, 30 at the head in that order?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}
void insertAtHead(int val) {
Node* newNode = new Node(val);
newNode->next = head;
head = newNode;
}
void printList() {
Node* temp = head;
while (temp != nullptr) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "null" << std::endl;
}
};
int main() {
LinkedList list;
list.insertAtHead(10);
list.insertAtHead(20);
list.insertAtHead(30);
list.printList();
return 0;
}Attempts:
2 left
💡 Hint
Remember, inserting at head means new nodes become the first element.
✗ Incorrect
Each new node is added before the current head, so the last inserted node appears first.
❓ Predict Output
intermediateOutput after appending nodes at the tail
What is the printed state of the singly linked list after appending nodes 5, 15, 25 at the tail in that order?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}
void appendAtTail(int val) {
Node* newNode = new Node(val);
if (head == nullptr) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
void printList() {
Node* temp = head;
while (temp != nullptr) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "null" << std::endl;
}
};
int main() {
LinkedList list;
list.appendAtTail(5);
list.appendAtTail(15);
list.appendAtTail(25);
list.printList();
return 0;
}Attempts:
2 left
💡 Hint
Appending at tail adds nodes to the end of the list.
✗ Incorrect
Nodes are added at the end, so the order remains 5, then 15, then 25.
🔧 Debug
advancedIdentify the bug in node deletion
What error occurs when trying to delete a node with value 10 from the list, given the code below?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}
void deleteNode(int val) {
Node* temp = head;
Node* prev = nullptr;
while (temp != nullptr && temp->data != val) {
prev = temp;
temp = temp->next;
}
if (temp == nullptr) return;
if (prev == nullptr) {
head = temp->next;
} else {
prev->next = temp->next;
}
// Missing delete temp;
}
};
int main() {
LinkedList list;
list.head = new Node(10);
list.head->next = new Node(20);
list.deleteNode(10);
return 0;
}Attempts:
2 left
💡 Hint
Check if the removed node's memory is freed.
✗ Incorrect
The code removes the node from the list but does not free its memory, causing a memory leak.
❓ Predict Output
advancedOutput after reversing a singly linked list
What is the printed state of the singly linked list after reversing the list 1 -> 2 -> 3 -> null?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}
void appendAtTail(int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next) temp = temp->next;
temp->next = newNode;
}
void reverse() {
Node* prev = nullptr;
Node* current = head;
Node* next = nullptr;
while (current) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
void printList() {
Node* temp = head;
while (temp) {
std::cout << temp->data << " -> ";
temp = temp->next;
}
std::cout << "null" << std::endl;
}
};
int main() {
LinkedList list;
list.appendAtTail(1);
list.appendAtTail(2);
list.appendAtTail(3);
list.reverse();
list.printList();
return 0;
}Attempts:
2 left
💡 Hint
Reversing flips the order of nodes.
✗ Incorrect
The reverse function changes the direction of links, so the last node becomes the head.
🧠 Conceptual
expertTime complexity of searching in singly linked list
What is the time complexity of searching for a value in a singly linked list with n nodes?
Attempts:
2 left
💡 Hint
You may need to check each node until you find the value or reach the end.
✗ Incorrect
In the worst case, you must check every node, so the time is proportional to n.
