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
