Question 01 of 05

Singly Linked List Traversal

Write a program to create and traverse a singly linked list.

C++
#include <iostream>
using namespace std;

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

void append(Node*& head, int val) {
    Node* newNode = new Node();
    newNode->data = val;
    newNode->next = NULL;
    if (head == NULL) { head = newNode; return; }
    Node* temp = head;
    while (temp->next != NULL) temp = temp->next;
    temp->next = newNode;
}

void display(Node* head) {
    while (head != NULL) { cout << head->data << " -> "; head = head->next; }
    cout << "NULL\n";
}

int main() {
    Node* head = NULL;
    append(head, 10); append(head, 20); append(head, 30);
    display(head);
    return 0;
}
Question 1 of 5