Linked List February 11 ,2026

Print a Singly Linked List

Problem Statement

Given the head pointer of a singly linked list, print all the elements of the linked list in sequential order starting from the head node.

The output should display the data stored in each node until the end of the list is reached.

What Does “Printing a Linked List” Mean?

Printing a linked list means traversing the list from the head node to the last node and displaying the data of each node.

Since a singly linked list does not support random access like arrays, we cannot directly access elements using an index. Instead, we must move step-by-step from one node to the next using the next pointer.

Example linked list:

Head

[10 | * ] → [20 | * ] → [30 | NULL]

Expected Output:

10 20 30

Why Printing a Linked List Is Important

Printing is one of the most fundamental linked list operations. It helps in:

  • Debugging linked list operations
  • Verifying insertion and deletion logic
  • Understanding traversal mechanics
  • Building intuition for more complex problems

Printing teaches traversal, and traversal is the foundation of almost every linked list algorithm.

Observational Thinking Before Coding

To print all nodes:

  • We must start from the head.
  • We must move sequentially.
  • We must stop when we reach NULL.
  • We must print each node’s data exactly once.

Key Insight:
Traversal is mandatory. There is no shortcut.

Approaches to Print a Linked List

There are two standard approaches:

  • Iterative Approach
  • Recursive Approach

Both will print elements in forward order.

Iterative Method to Print a Linked List

Conceptual Understanding

In the iterative approach:

  • A temporary pointer is created.
  • It starts at the head.
  • While the pointer is not NULL:
    • Print the data.
    • Move to the next node.

This method uses a loop and is memory efficient.

Algorithm

If head is NULL:
Print “List is empty”
Return

Set temp = head

While temp != NULL:
Print temp->data
Move temp = temp->next

Time and Space Complexity

Time Complexity: O(n)
Space Complexity: O(1)

We visit each node once and use constant extra space.

C Implementation

#include 
#include 

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

void printList(struct Node* head) {
    if (head == NULL) {
        printf("List is empty\n");
        return;
    }

    struct Node* temp = head;

    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }

    printf("\n");
}

int main() {

    struct Node* head = (struct Node*)malloc(sizeof(struct Node));
    head->data = 10;

    head->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->data = 20;

    head->next->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->next->data = 30;

    head->next->next->next = NULL;

    printf("Linked List Elements: ");
    printList(head);

    return 0;
}

Output

Linked List Elements: 10 20 30

C++ Implementation

#include 
using namespace std;

class Node {
public:
    int data;
    Node* next;

    Node(int val) {
        data = val;
        next = NULL;
    }
};

void printList(Node* head) {
    if (head == NULL) {
        cout << "List is empty" << endl;
        return;
    }

    Node* temp = head;

    while (temp != NULL) {
        cout << temp->data << " ";
        temp = temp->next;
    }

    cout << endl;
}

int main() {

    Node* head = new Node(10);
    head->next = new Node(20);
    head->next->next = new Node(30);

    cout << "Linked List Elements: ";
    printList(head);

    return 0;
}

Output

Linked List Elements: 10 20 30

Java Implementation

class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

public class Main {

    public static void printList(Node head) {
        if (head == null) {
            System.out.println("List is empty");
            return;
        }

        Node temp = head;

        while (temp != null) {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }

        System.out.println();
    }

    public static void main(String[] args) {

        Node head = new Node(10);
        head.next = new Node(20);
        head.next.next = new Node(30);

        System.out.print("Linked List Elements: ");
        printList(head);
    }
}

Output

Linked List Elements: 10 20 30

Python Implementation

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def print_list(head):

    if head is None:
        print("List is empty")
        return

    temp = head

    while temp is not None:
        print(temp.data, end=" ")
        temp = temp.next

    print()


# Creating Linked List
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)

print("Linked List Elements:", end=" ")
print_list(head)

Output

Linked List Elements: 10 20 30

Recursive Method to Print a Linked List

Conceptual Understanding

In recursion:

  • Print the current node.
  • Recursively call the function for the next node.
  • Stop when the node becomes NULL.

This method uses the call stack to manage traversal.

Recursive Formula

If head == NULL:
return

Print head->data
Call print(head->next)

Time and Space Complexity

Time Complexity: O(n)
Space Complexity: O(n) due to recursion stack

C Implementation

#include 
#include 

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

void printListRecursive(struct Node* head) {

    if (head == NULL)
        return;

    printf("%d ", head->data);
    printListRecursive(head->next);
}

int main() {

    struct Node* head = (struct Node*)malloc(sizeof(struct Node));
    head->data = 10;

    head->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->data = 20;

    head->next->next = (struct Node*)malloc(sizeof(struct Node));
    head->next->next->data = 30;

    head->next->next->next = NULL;

    printf("Linked List Elements: ");
    printListRecursive(head);
    printf("\n");

    return 0;
}

Output

Linked List Elements: 10 20 30

C++ Implementation

#include 
using namespace std;

class Node {
public:
    int data;
    Node* next;

    Node(int val) {
        data = val;
        next = NULL;
    }
};

void printListRecursive(Node* head) {

    if (head == NULL)
        return;

    cout << head->data << " ";
    printListRecursive(head->next);
}

int main() {

    Node* head = new Node(10);
    head->next = new Node(20);
    head->next->next = new Node(30);

    cout << "Linked List Elements: ";
    printListRecursive(head);
    cout << endl;

    return 0;
}

Output

Linked List Elements: 10 20 30

Java Implementation

class Node {
    int data;
    Node next;

    Node(int data) {
        this.data = data;
        this.next = null;
    }
}

public class Main {

    public static void printListRecursive(Node head) {

        if (head == null)
            return;

        System.out.print(head.data + " ");
        printListRecursive(head.next);
    }

    public static void main(String[] args) {

        Node head = new Node(10);
        head.next = new Node(20);
        head.next.next = new Node(30);

        System.out.print("Linked List Elements: ");
        printListRecursive(head);
    }
}

Output

Linked List Elements: 10 20 30

Python Implementation

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

def print_list_recursive(head):

    if head is None:
        return

    print(head.data, end=" ")
    print_list_recursive(head.next)


head = Node(10)
head.next = Node(20)
head.next.next = Node(30)

print("Linked List Elements:", end=" ")
print_list_recursive(head)

Output

Linked List Elements: 10 20 30

Next Problem in the series:

Search in a Linked List

Sanjiv
0

You must logged in to post comments.

Related Blogs

Introducti...
Linked List January 01 ,2026

Introduction to Link...

Length of...
Linked List January 01 ,2026

Length of a Singly L...

Remove Eve...
Linked List January 01 ,2026

Remove Every K-th No...

Middle of...
Linked List January 01 ,2026

Middle of a Linked L...

Count Occu...
Linked List January 01 ,2026

Count Occurrences in...

Circular L...
Linked List January 01 ,2026

Circular Linked List...

Check if a...
Linked List January 01 ,2026

Check if a Linked Li...

Count Node...
Linked List January 01 ,2026

Count Nodes in a Cir...

Deletion f...
Linked List January 01 ,2026

Deletion from a Circ...

Singly to...
Linked List January 01 ,2026

Singly to Circular L...

Exchange F...
Linked List January 01 ,2026

Exchange First and L...

Deletion i...
Linked List January 01 ,2026

Deletion in a Doubly...

Search in...
Linked List February 02 ,2026

Search in a Singly L...

Linked Lis...
Linked List February 02 ,2026

Linked List Insertio...

Deleting a...
Linked List February 02 ,2026

Deleting a Given Key...

Deleting a...
Linked List February 02 ,2026

Deleting a Node at a...

Delete Ent...
Linked List February 02 ,2026

Delete Entire Linked...

Nth Node f...
Linked List February 02 ,2026

Nth Node from Start

Nth Node f...
Linked List February 02 ,2026

Nth Node from End

Size of Do...
Linked List February 02 ,2026

Size of Doubly Linke...

Reverse a...
Linked List February 02 ,2026

Reverse a Singly Lin...

Reverse a...
Linked List February 02 ,2026

Reverse a Doubly Lin...

Insert at...
Linked List February 02 ,2026

Insert at Beginning...

Insert at...
Linked List February 02 ,2026

Insert at End of Dou...

Delete Fir...
Linked List February 02 ,2026

Delete First Node of...

Check if L...
Linked List February 02 ,2026

Check if Linked List...

Copy a Lin...
Linked List February 02 ,2026

Copy a Linked List

Swap Nodes...
Linked List February 02 ,2026

Swap Nodes in Pairs

Detect Loo...
Linked List February 02 ,2026

Detect Loop in a Lin...

Length of...
Linked List February 02 ,2026

Length of Loop in Li...

Design Bro...
Linked List February 02 ,2026

Design Browser Histo...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com