Bird
Raised Fist0
DSA Cprogramming~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 Traversal Master
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
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

void printList(struct Node* head) {
    struct Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node* head = malloc(sizeof(struct Node));
    struct Node* second = malloc(sizeof(struct Node));
    struct Node* third = malloc(sizeof(struct Node));

    head->data = 10;
    head->next = second;

    second->data = 20;
    second->next = third;

    third->data = 30;
    third->next = NULL;

    printList(head);
    return 0;
}
ANULL
B10 -> 20 -> NULL
C20 -> 30 -> NULL
D10 -> 20 -> 30 -> NULL
Attempts:
2 left
💡 Hint
Follow the next pointers from head until NULL is reached.
❓ Predict Output
intermediate
2:00remaining
Output After Modifying Linked List Pointer
What will be the output of the following code that modifies the linked list before printing?
DSA C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

void printList(struct Node* head) {
    struct Node* current = head;
    while (current != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("NULL\n");
}

int main() {
    struct Node* head = malloc(sizeof(struct Node));
    struct Node* second = malloc(sizeof(struct Node));
    struct Node* third = malloc(sizeof(struct Node));

    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = NULL;

    // Modify the list
    second->next = NULL;

    printList(head);
    return 0;
}
A1 -> 2 -> 3 -> NULL
B1 -> 2 -> NULL
C2 -> 3 -> NULL
D1 -> 3 -> NULL
Attempts:
2 left
💡 Hint
Check how the next pointer of the second node is changed.
🔧 Debug
advanced
2:00remaining
Identify the Error in Linked List Traversal
What error will occur when running this code that attempts to print a linked list?
DSA C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

void printList(struct Node* head) {
    struct Node* current = head;
    while (current->next != NULL) {
        printf("%d -> ", current->data);
        current = current->next;
    }
    printf("%d -> NULL\n", current->data);
}

int main() {
    struct Node* head = malloc(sizeof(struct Node));
    head->data = 5;
    head->next = NULL;

    printList(head);
    return 0;
}
ANo error, output: 5 -> NULL
BSegmentation fault (accessing NULL pointer)
CInfinite loop
DCompilation error
Attempts:
2 left
💡 Hint
Check the loop condition and how it handles single-node list.
❓ Predict Output
advanced
2:00remaining
Output of Recursive Linked List Print
What is the output of this recursive function printing a linked list?
DSA C
#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node* next;
};

void printListRecursive(struct Node* node) {
    if (node == NULL) {
        printf("NULL\n");
        return;
    }
    printf("%d -> ", node->data);
    printListRecursive(node->next);
}

int main() {
    struct Node* head = malloc(sizeof(struct Node));
    struct Node* second = malloc(sizeof(struct Node));

    head->data = 7;
    head->next = second;

    second->data = 14;
    second->next = NULL;

    printListRecursive(head);
    return 0;
}
A14 -> 7 -> NULL
B7 -> 14 ->
C7 -> 14 -> NULL
DNULL
Attempts:
2 left
💡 Hint
Think about how recursion prints nodes in order.
🧠 Conceptual
expert
2:00remaining
Number of Nodes After Partial Traversal
Given a linked list with 5 nodes, if you traverse the list with the following code snippet, how many nodes will be counted?
DSA C
int countNodes(struct Node* head) {
    int count = 0;
    struct Node* current = head;
    while (current != NULL && count < 3) {
        count++;
        current = current->next;
    }
    return count;
}
A3
B5
C0
D4
Attempts:
2 left
💡 Hint
The loop stops when count reaches 3 or list ends.

Practice

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

Solution

  1. Step 1: Understand traversal meaning

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

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

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

    Traversal = Visit nodes [OK]
Hint: Traversal means visiting nodes to read data [OK]
Common Mistakes:
  • Confusing traversal with deletion
  • Thinking traversal creates new nodes
  • Assuming traversal reverses the list
2. Which of the following is the correct syntax to start traversing a linked list named head in C?
easy
A. Node* temp = &head;
B. struct Node temp = head;
C. struct Node* temp = head;
D. Node temp = head;

Solution

  1. Step 1: Understand pointer usage

    In C, linked list nodes are accessed via pointers, so temp must be a pointer to Node.
  2. Step 2: Check syntax correctness

    struct Node* temp = head; correctly declares temp as a pointer and assigns it to head.
  3. Final Answer:

    struct Node* temp = head; -> Option C
  4. Quick Check:

    Pointer assignment = struct Node* temp = head [OK]
Hint: Use pointer variable to traverse linked list [OK]
Common Mistakes:
  • Using struct Node instead of pointer
  • Taking address of head incorrectly
  • Missing struct keyword if needed
3. What will be the output of the following C code snippet that traverses and prints a linked list?
struct Node {
  int data;
  struct Node* next;
};

int main() {
  struct Node n3 = {30, NULL};
  struct Node n2 = {20, &n3};
  struct Node n1 = {10, &n2};
  struct Node* head = &n1;

  struct Node* temp = head;
  while (temp != NULL) {
    printf("%d -> ", temp->data);
    temp = temp->next;
  }
  printf("NULL\n");
  return 0;
}
medium
A. 10 -> 20 -> 30 -> NULL
B. 30 -> 20 -> 10 -> NULL
C. 10 20 30 NULL
D. Compilation error

Solution

  1. Step 1: Trace linked list nodes

    The list nodes are n1(10) -> n2(20) -> n3(30) -> NULL.
  2. Step 2: Follow traversal and print

    The loop prints each node's data followed by ' -> ', then prints 'NULL' at the end.
  3. Final Answer:

    10 -> 20 -> 30 -> NULL -> Option A
  4. Quick Check:

    Print nodes in order = 10 -> 20 -> 30 -> NULL [OK]
Hint: Follow next pointers to print nodes in order [OK]
Common Mistakes:
  • Printing nodes in reverse order
  • Missing '->' arrows in output
  • Forgetting to print NULL at end
4. Identify the error in the following code snippet that attempts to print a linked list:
struct Node {
  int data;
  struct Node* next;
};

void printList(struct Node* head) {
  while (head->next != NULL) {
    printf("%d -> ", head->data);
    head = head->next;
  }
  printf("NULL\n");
}
medium
A. head should be a double pointer
B. Missing semicolon after printf
C. printf format specifier is incorrect
D. The loop condition should be head != NULL

Solution

  1. Step 1: Analyze loop condition

    The loop stops when head->next is NULL, so it skips printing the last node.
  2. Step 2: Correct loop condition

    Changing condition to head != NULL ensures all nodes, including last, are printed.
  3. Final Answer:

    The loop condition should be head != NULL -> Option D
  4. Quick Check:

    Loop until head is NULL to print all nodes [OK]
Hint: Loop until pointer is NULL, not next pointer [OK]
Common Mistakes:
  • Stopping loop too early
  • Using wrong pointer type
  • Ignoring last node in print
5. Given a linked list where some nodes may have data value 0, which of the following code snippets correctly prints all nodes including those with 0?
struct Node* temp = head;
while (temp) {
  if (temp->data) {
    printf("%d -> ", temp->data);
  }
  temp = temp->next;
}
printf("NULL\n");
hard
A. Change 'if (temp->data)' to always print without condition
B. Add 'temp = temp->next;' inside the if block only
C. Use 'while (temp != NULL)' and keep the if condition
D. Replace 'printf' with 'puts' to print zeros

Solution

  1. Step 1: Understand the if condition effect

    The condition 'if (temp->data)' skips printing nodes with data 0 because 0 is false.
  2. Step 2: Fix to print all nodes

    Removing the if condition prints all nodes including those with 0.
  3. Final Answer:

    Change 'if (temp->data)' to always print without condition -> Option A
  4. Quick Check:

    Print all nodes regardless of data value [OK]
Hint: Do not skip nodes with zero data when printing [OK]
Common Mistakes:
  • Skipping zero values due to if condition
  • Moving pointer only inside if block causing infinite loop
  • Using wrong print function for integers
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