Bird
Raised Fist0
DSA C++programming~20 mins

Traversal and Printing a Linked List in DSA C++ - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Linked List Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Traversing a Linked List
What is the printed output of the following C++ code that traverses and prints a singly linked list?
DSA C++
struct Node {
    int data;
    Node* next;
};

int main() {
    Node n3 = {30, nullptr};
    Node n2 = {20, &n3};
    Node n1 = {10, &n2};

    Node* head = &n1;
    while (head != nullptr) {
        std::cout << head->data << " -> ";
        head = head->next;
    }
    std::cout << "null" << std::endl;
    return 0;
}
A10 20 30 null
B30 -> 20 -> 10 -> null
Cnull -> 10 -> 20 -> 30
D10 -> 20 -> 30 -> null
Attempts:
2 left
💡 Hint
Remember the traversal starts from the head and moves to next nodes until nullptr.
❓ Predict Output
intermediate
2:00remaining
Output After Modifying Linked List Pointer
What is the output of this C++ code that modifies the head pointer before traversal?
DSA C++
struct Node {
    int data;
    Node* next;
};

int main() {
    Node n3 = {30, nullptr};
    Node n2 = {20, &n3};
    Node n1 = {10, &n2};

    Node* head = &n2; // start from second node
    while (head != nullptr) {
        std::cout << head->data << " -> ";
        head = head->next;
    }
    std::cout << "null" << std::endl;
    return 0;
}
A20 -> 30 -> null
B10 -> 20 -> 30 -> null
C30 -> null
Dnull
Attempts:
2 left
💡 Hint
The traversal starts from the node with data 20, not 10.
🔧 Debug
advanced
2:00remaining
Identify the Runtime Error in Linked List Traversal
What runtime error will occur when running this C++ code that attempts to traverse a linked list?
DSA C++
struct Node {
    int data;
    Node* next;
};

int main() {
    Node* head = nullptr;
    while (head->next != nullptr) {
        std::cout << head->data << " -> ";
        head = head->next;
    }
    std::cout << "null" << std::endl;
    return 0;
}
ASegmentation fault (accessing nullptr)
BNo output, program ends normally
CCompilation error: 'head' undeclared
DInfinite loop
Attempts:
2 left
💡 Hint
Check what happens when head is nullptr and you try to access head->next.
❓ Predict Output
advanced
2:00remaining
Output of Recursive Linked List Print
What is the output of this recursive function that prints a linked list?
DSA C++
struct Node {
    int data;
    Node* next;
};

void printList(Node* node) {
    if (node == nullptr) {
        std::cout << "null" << std::endl;
        return;
    }
    std::cout << node->data << " -> ";
    printList(node->next);
}

int main() {
    Node n3 = {30, nullptr};
    Node n2 = {20, &n3};
    Node n1 = {10, &n2};
    printList(&n1);
    return 0;
}
Anull -> 10 -> 20 -> 30
B10 -> 20 -> 30 -> null
C10 20 30 null
D30 -> 20 -> 10 -> null
Attempts:
2 left
💡 Hint
The function prints current node data then calls itself on next node.
🧠 Conceptual
expert
2:00remaining
Number of Nodes in a Linked List After Traversal
Given a singly linked list with nodes containing values 5, 15, 25, 35 linked in order, what is the number of nodes counted after traversing the list from head to end?
A3
B0
C4
D5
Attempts:
2 left
💡 Hint
Count each node visited until you reach nullptr.

Practice

(1/5)
1. What is the main purpose of traversing a linked list in data structures?
easy
A. To delete all nodes at once
B. To reverse the linked list automatically
C. To create a new linked list
D. To visit each node and access its data

Solution

  1. Step 1: Understand traversal meaning

    Traversal means visiting each node one by one in the linked list.
  2. Step 2: Purpose of traversal

    The main goal is to access or process the data stored in each node during the visit.
  3. Final Answer:

    To visit each node and access its data -> Option D
  4. Quick Check:

    Traversal = Visit nodes [OK]
Hint: Traversal means visiting nodes one by one [OK]
Common Mistakes:
  • Confusing traversal with deletion
  • Thinking traversal creates new lists
  • Assuming traversal reverses the list
2. Which of the following is the correct syntax to start traversing a singly linked list in C++ if head is the pointer to the first node?
easy
A. Node* current = head;
B. Node current = head;
C. Node* current = &head;
D. Node current = *head;

Solution

  1. Step 1: Understand pointer usage

    In C++, linked list nodes are accessed via pointers. head is a pointer to the first node.
  2. Step 2: Correct pointer assignment

    To traverse, we create a pointer current and assign it to head directly: Node* current = head;
  3. Final Answer:

    Node* current = head; -> Option A
  4. Quick Check:

    Use pointer to start traversal [OK]
Hint: Use pointer assignment to start traversal [OK]
Common Mistakes:
  • Using object instead of pointer
  • Taking address of head incorrectly
  • Dereferencing head wrongly
3. What will be the output of the following C++ code snippet that prints a linked list?
struct Node {
  int data;
  Node* next;
};

int main() {
  Node n3 = {3, nullptr};
  Node n2 = {2, &n3};
  Node n1 = {1, &n2};
  Node* head = &n1;

  Node* current = head;
  while (current != nullptr) {
    std::cout << current->data << " -> ";
    current = current->next;
  }
  std::cout << "null" << std::endl;
  return 0;
}
medium
A. null -> 1 -> 2 -> 3
B. 3 -> 2 -> 1 -> null
C. 1 -> 2 -> 3 -> null
D. 1 2 3 null

Solution

  1. Step 1: Trace node connections

    Node n1 points to n2, n2 points to n3, n3 points to nullptr (end).
  2. Step 2: Follow the while loop output

    The loop prints data of current node then moves to next. So prints 1, then 2, then 3, then stops.
  3. Final Answer:

    1 -> 2 -> 3 -> null -> Option C
  4. Quick Check:

    Print nodes in order from head [OK]
Hint: Follow next pointers from head until nullptr [OK]
Common Mistakes:
  • Printing nodes in reverse order
  • Missing '->' arrows in output
  • Not printing 'null' at the end
4. Identify the error in the following code snippet that attempts to print a linked list:
struct Node {
  int data;
  Node* next;
};

void printList(Node* head) {
  Node* current = head;
  while (current->next != nullptr) {
    std::cout << current->data << " -> ";
    current = current->next;
  }
  std::cout << "null" << std::endl;
}
medium
A. The loop condition should be current != nullptr, not current->next != nullptr
B. The print statement should be after the loop
C. The function should return an int
D. The pointer current should be declared as Node current, not Node* current

Solution

  1. Step 1: Analyze loop condition

    The loop stops when current->next is nullptr, so it skips printing the last node's data.
  2. Step 2: Correct loop condition

    It should check current != nullptr to print all nodes including the last one.
  3. Final Answer:

    The loop condition should be current != nullptr, not current->next != nullptr -> Option A
  4. Quick Check:

    Loop until current is nullptr to print all nodes [OK]
Hint: Loop until current is nullptr to include last node [OK]
Common Mistakes:
  • Stopping loop too early missing last node
  • Wrong pointer type declaration
  • Misplacing print statement
5. Given a singly linked list, how would you modify the traversal code to print the list in reverse order without changing the list structure or using recursion?
hard
A. Traverse normally and print nodes as you go
B. Use a stack to store nodes during traversal, then pop and print
C. Reverse the list first, then print
D. Use two pointers to print backwards

Solution

  1. Step 1: Understand constraints

    We cannot change the list or use recursion, so reversing pointers or recursive calls are not allowed.
  2. Step 2: Use auxiliary data structure

    Use a stack to push each node's data during traversal, then pop from stack to print in reverse order.
  3. Final Answer:

    Use a stack to store nodes during traversal, then pop and print -> Option B
  4. Quick Check:

    Stack reverses order without modifying list [OK]
Hint: Use stack to reverse print without changing list [OK]
Common Mistakes:
  • Trying to reverse list pointers directly
  • Using recursion despite restriction
  • Assuming two pointers can print backwards
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