Bird
Raised Fist0
DSA C++programming~20 mins

Push Using Linked List Node in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Push Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What 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;
}
A20 -> 30 -> 10 -> null
B30 -> 20 -> 10 -> null
Cnull
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember, pushing a node adds it to the front of the list.
❓ Predict Output
intermediate
2:00remaining
What 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;
}
A15 -> 25 -> 5 -> null
B5 -> 15 -> 25 -> null
C25 -> 15 -> 5 -> null
Dnull
Attempts:
2 left
💡 Hint
New nodes become the new head of the list.
🔧 Debug
advanced
2:00remaining
Why 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;
}
ABecause head is not updated correctly, the list remains empty.
BBecause the method does not check if newNode is nullptr, it causes a null pointer dereference.
CBecause newNode is not allocated dynamically, it causes a memory error.
DBecause newNode->next is set to nullptr, it breaks the existing list links causing loss of nodes.
Attempts:
2 left
💡 Hint
Think about what happens to the existing list when you push a new node.
🧠 Conceptual
advanced
1:00remaining
What 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?
AO(1) - Constant time
BO(n) - Linear time
CO(log n) - Logarithmic time
DO(n^2) - Quadratic time
Attempts:
2 left
💡 Hint
Pushing to the front does not require traversing the list.
🚀 Application
expert
3:00remaining
After 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;
}
A4 -> 3 -> 2 -> 1 -> null
B1 -> 2 -> 3 -> 4 -> null
C1 -> 4 -> 3 -> 2 -> null
Dnull
Attempts:
2 left
💡 Hint
Each push adds the new node at the front, so the last pushed node is the head.

Practice

(1/5)
1. What does the push operation do in a linked list when using nodes?
easy
A. Deletes the entire linked list
B. Removes the last node from the list
C. Searches for a node with a specific value
D. Adds a new node at the beginning of the list

Solution

  1. Step 1: Understand push operation in linked list

    The push operation inserts a new node at the start (head) of the linked list.
  2. Step 2: Effect of push on list structure

    The new node becomes the head, and its next pointer points to the previous head node.
  3. Final Answer:

    Adds a new node at the beginning of the list -> Option D
  4. Quick Check:

    Push means insert at head = D [OK]
Hint: Push always adds node at the start (head) [OK]
Common Mistakes:
  • Confusing push with pop (removal)
  • Thinking push adds at the end
  • Mixing push with search or delete
2. Which of the following is the correct syntax to create a new node and push it at the head in C++ linked list?
easy
A. Node* newNode = new Node(data); newNode->next = head; head = newNode;
B. Node newNode = Node(data); head = newNode.next;
C. newNode = Node(data); newNode.next = head; head = newNode;
D. Node* newNode = Node(data); head = newNode->next;

Solution

  1. Step 1: Create new node dynamically

    Use new Node(data) to allocate memory and initialize node.
  2. Step 2: Link new node to current head and update head

    Set newNode->next = head and then head = newNode to push at front.
  3. Final Answer:

    Node* newNode = new Node(data); newNode->next = head; head = newNode; -> Option A
  4. Quick Check:

    Dynamic allocation + pointer update = B [OK]
Hint: Use pointer and 'new' keyword to create and link node [OK]
Common Mistakes:
  • Not using pointer (*) for new node
  • Using dot (.) instead of arrow (->) for pointers
  • Assigning head incorrectly
3. What will be the output linked list after executing this code?
struct Node {
  int data;
  Node* next;
  Node(int val) : data(val), next(nullptr) {}
};

Node* head = nullptr;

// Push 10
Node* newNode1 = new Node(10);
newNode1->next = head;
head = newNode1;

// Push 20
Node* newNode2 = new Node(20);
newNode2->next = head;
head = newNode2;

// Push 30
Node* newNode3 = new Node(30);
newNode3->next = head;
head = newNode3;

// Print list
Node* temp = head;
while(temp != nullptr) {
  std::cout << temp->data << " -> ";
  temp = temp->next;
}
std::cout << "null";
medium
A. 10 -> 20 -> 30 -> null
B. 30 -> 20 -> 10 -> null
C. 20 -> 30 -> 10 -> null
D. null

Solution

  1. Step 1: Trace push operations

    First push 10: list is 10 -> null. Then push 20: list becomes 20 -> 10 -> null. Then push 30: list becomes 30 -> 20 -> 10 -> null.
  2. Step 2: Print linked list

    Printing from head prints 30, then 20, then 10, then null.
  3. Final Answer:

    30 -> 20 -> 10 -> null -> Option B
  4. Quick Check:

    Push adds at head, last pushed is first = C [OK]
Hint: Last pushed node appears first in list [OK]
Common Mistakes:
  • Assuming push adds at tail
  • Mixing order of nodes
  • Not updating head pointer correctly
4. Identify the error in this push function for a singly linked list:
void push(Node* head, int data) {
  Node* newNode = new Node(data);
  newNode->next = head;
  head = newNode;
}
medium
A. head is passed by value, so original list head is not updated
B. newNode is not allocated dynamically
C. newNode->next should be nullptr
D. Function should return void* instead of void

Solution

  1. Step 1: Analyze parameter passing

    head is passed by value, so changes inside function do not affect original head pointer.
  2. Step 2: Effect on linked list

    Assigning head = newNode only changes local copy; original list head remains unchanged.
  3. Final Answer:

    head is passed by value, so original list head is not updated -> Option A
  4. Quick Check:

    Pass pointer by reference or pointer to pointer [OK]
Hint: Pass head by reference or pointer to update original [OK]
Common Mistakes:
  • Ignoring parameter passing method
  • Not returning new head if passed by value
  • Incorrect pointer usage
5. You want to implement a stack using a singly linked list with push operation. Which approach correctly maintains the stack's LIFO order?
hard
A. Push new nodes at the tail and update head pointer
B. Push new nodes at the tail of the linked list without tail pointer
C. Push new nodes at the head of the linked list
D. Insert new nodes randomly in the list

Solution

  1. Step 1: Understand stack LIFO behavior

    Stack requires last-in, first-out order, so newest element must be accessed first.
  2. Step 2: Push at head maintains LIFO

    Adding new nodes at head means newest node is always at front, enabling O(1) push and pop.
  3. Step 3: Problems with tail insertion

    Inserting at tail without tail pointer is inefficient and does not maintain LIFO easily.
  4. Final Answer:

    Push new nodes at the head of the linked list -> Option C
  5. Quick Check:

    Stack push = insert at head = A [OK]
Hint: Stack push uses head insertion for O(1) LIFO [OK]
Common Mistakes:
  • Pushing at tail without tail pointer (slow)
  • Not maintaining LIFO order
  • Inserting nodes randomly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard