Challenge - 5 Problems
Linked List Push Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after pushing nodes in this linked list?
Consider the following C++ code that pushes nodes to the front of a singly linked list. What will be the printed linked list after all push operations?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}
void push(Node* newNode) {
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.push(new Node(10));
list.push(new Node(20));
list.push(new Node(30));
list.printList();
return 0;
}Attempts:
2 left
💡 Hint
Remember, pushing a node adds it to the front of the list.
✗ Incorrect
Each push adds the new node at the head, so the last pushed node appears first in the list.
❓ Predict Output
intermediateWhat is the linked list after pushing nodes with these values?
Given the push method that takes a Node pointer and adds it to the front, what will be the linked list after pushing nodes with values 5, 15, and 25 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 push(Node* newNode) {
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.push(new Node(5));
list.push(new Node(15));
list.push(new Node(25));
list.printList();
return 0;
}Attempts:
2 left
💡 Hint
New nodes become the new head of the list.
✗ Incorrect
Pushing nodes adds them at the front, so the last pushed node is the head.
🔧 Debug
advancedWhy does this push method cause a runtime error?
Examine the push method below. It is supposed to add a new node to the front of the linked list. Why does it cause a runtime error when called?
DSA C++
void push(Node* newNode) {
newNode->next = nullptr;
head = newNode;
}Attempts:
2 left
💡 Hint
Think about what happens to the existing list when you push a new node.
✗ Incorrect
Setting newNode->next to nullptr disconnects the new node from the existing list, losing all previous nodes.
🧠 Conceptual
advancedWhat is the time complexity of pushing a node to the front of a singly linked list?
Consider a singly linked list where you add a new node to the front using a push method. What is the time complexity of this operation?
Attempts:
2 left
💡 Hint
Pushing to the front does not require traversing the list.
✗ Incorrect
Pushing a node to the front only changes pointers at the head, so it takes constant time.
🚀 Application
expertAfter pushing nodes 1, 2, 3, 4 in order, what is the linked list?
You push nodes with values 1, 2, 3, and 4 in that order to an empty singly linked list using the push method that takes a Node pointer. What is the final linked list?
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
class LinkedList {
public:
Node* head;
LinkedList() : head(nullptr) {}
void push(Node* newNode) {
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.push(new Node(1));
list.push(new Node(2));
list.push(new Node(3));
list.push(new Node(4));
list.printList();
return 0;
}Attempts:
2 left
💡 Hint
Each push adds the new node at the front, so the last pushed node is the head.
✗ Incorrect
Nodes are added to the front, so the list order is reversed from the push order.
