Traversal of Singly Linked List (original) (raw)
Last Updated : 27 Aug, 2025
Traversal of Singly Linked List is one of the fundamental operations, where we traverse or visit each node of the linked list. In this article, we will cover how to traverse all the nodes of a singly linked list along with its implementation.
**Examples:
**Input:
**Output: 1 -> 2 -> 3 -> 4 -> 5
**Explanation: Every element of each node from head node to last node is printed which means we have traversed each node successfully.**Input:
**Output: 10 -> 20 -> 30 -> 40 -> 50
**Explanation: Every element of each node from head node to last node is printed which means we have traversed each node successfully.
Table of Content
- Traversal of Singly Linked List (Iterative Approach)
- Traversal of Singly Linked List (Recursive Approach)
Traversal of Singly Linked List (Iterative Approach)
The process of traversing a singly linked list involves printing the value of each node and then going on to the next node and print that node's value also and so on, till we reach the last node in the singly linked list, whose next node points towards the null.
**Step-by-Step Algorithm:
- We will initialize a temporary pointer to the head node of the singly linked list.
- After that, we will check if that pointer is null or not null, if it is null, then return.
- While the pointer is not null, we will access and print the data of the current node, then we move the pointer to next node. C++ `
#include using namespace std;
// a linked list node class Node { public: int data; Node* next;
// constructor to initialize a new node with data
Node(int new_data) {
this->data = new_data;
this->next = nullptr;
}};
// function to traverse and print the singly linked list
void traverseList(Node* head) {
while (head != nullptr) {
cout << head->data;
if (head->next != nullptr)
cout << " -> ";
head = head->next;
}
cout << endl;
}
int main() {
// create a hard-coded linked list:
// 10 -> 20 -> 30 -> 40
Node* head = new Node(10);
head->next = new Node(20);
head->next->next = new Node(30);
head->next->next->next = new Node(40);
traverseList(head);
return 0;}
Java
// a linked list node class Node { int data; Node next;
// constructor to initialize a new node with data
Node(int new_data) {
this.data = new_data;
this.next = null;
}}
public class GfG {
// function to traverse and print the singly linked list
public static void traverseList(Node head) {
while (head != null) {
System.out.print(head.data);
if (head.next != null)
System.out.print(" -> ");
head = head.next;
}
System.out.println();
}
public static void main(String[] args) {
// create a hard-coded linked list:
// 10 -> 20 -> 30 -> 40
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
traverseList(head);
}}
Python
a linked list node
class Node:
# constructor to initialize a new node with data
def __init__(self, new_data):
self.data = new_data
self.next = Nonefunction to traverse and print the singly linked list
def traverseList(head):
while head is not None:
print(head.data, end="")
if head.next is not None:
print(" -> ", end="")
head = head.next
print()
if name == "main":
# create a hard-coded linked list:
# 10 -> 20 -> 30 -> 40
head = Node(10)
head.next = Node(20)
head.next.next = Node(30)
head.next.next.next = Node(40)
traverseList(head)C#
using System;
// a linked list node class Node { public int Data { get; set; } public Node Next { get; set; }
// constructor to initialize a new node with data
public Node(int new_data) {
Data = new_data;
Next = null;
}}
class GfG {
// function to traverse and print the singly linked list
static void TraverseList(Node head) {
while (head != null) {
Console.Write(head.Data);
if (head.Next != null) {
Console.Write(" -> ");
}
head = head.Next;
}
Console.WriteLine();
}
public static void Main(string[] args) {
// create a hard-coded linked list:
// 10 -> 20 -> 30 -> 40
Node head = new Node(10);
head.Next = new Node(20);
head.Next.Next = new Node(30);
head.Next.Next.Next = new Node(40);
TraverseList(head);
}}
JavaScript
// a linked list node class Node {
// constructor to initialize a new node with data
constructor(newData) {
this.data = newData;
this.next = null;
}}
// function to traverse and print the singly linked list function traverseList(head) {
while (head !== null) {
process.stdout.write(head.data.toString());
if (head.next !== null) {
process.stdout.write(" -> ");
}
head = head.next;
}
console.log();}
// Driver code
// create a hard-coded linked list: // 10 -> 20 -> 30 -> 40 let head = new Node(10); head.next = new Node(20); head.next.next = new Node(30); head.next.next.next = new Node(40);
traverseList(head);
`
Output
10 -> 20 -> 30 -> 40
**Time Complexity: O(n), where nis the number of nodes in the linked list.
**Auxiliary Space: O(1)
Traversal of Singly Linked List (Recursive Approach)
We can also traverse the singly linked list using recursion. We start at the head node of the singly linked list, check if it is null or not and print its value. We then call the traversal function again with the next node passed as pointer.
**Step-by-Step Algorithm:
- Firstly, we define a recursive method to traverse the singly linked list, which takes a node as a parameter.
- In this function, the base case is that if the node is null then we will return from the recursive method.
- We then pass the head node as the parameter to this function.
- After that, we access and print the data of the current node.
- At last, we will make a recursive call to this function with the next node as the parameter. C++ `
#include using namespace std;
// a linked list node class Node { public: int data; Node* next;
// constructor to initialize a new node with data
Node(int new_data) {
this->data = new_data;
this->next = nullptr;
}};
// function to traverse and print the singly linked list (recursive) void traverseList(Node* head) {
// base condition is when the head is nullptr
if (head == nullptr) {
cout << endl;
return;
}
// printing the current node data
cout << head->data;
// print arrow if not the last node
if (head->next != nullptr) {
cout << " -> ";
}
// moving to the next node
traverseList(head->next);}
int main() {
// create a hard-coded linked list:
// 10 -> 20 -> 30 -> 40
Node* head = new Node(10);
head->next = new Node(20);
head->next->next = new Node(30);
head->next->next->next = new Node(40);
traverseList(head);
return 0;}
Java
// a linked list node class Node { int data; Node next;
// constructor to initialize a new node with data
Node(int new_data) {
data = new_data;
next = null;
}}
public class GfG {
// function to traverse and print the singly linked list
static void traverseList(Node head) {
// base condition is when the head is null
if (head == null) {
System.out.println();
return;
}
// printing the current node data
System.out.print(head.data);
// print arrow if not the last node
if (head.next != null) {
System.out.print(" -> ");
}
// moving to the next node
traverseList(head.next);
}
public static void main(String[] args) {
// create a hard-coded linked list:
// 10 -> 20 -> 30 -> 40
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
traverseList(head);
}}
Python
a linked list node
class Node: def init(self, data): # constructor to initialize a new node with data self.data = data self.next = None
function to traverse and print the singly linked list
def traverseList(head): # base condition is when the head is None if head is None: print() return
# printing the current node data
print(head.data, end="")
# print arrow if not the last node
if head.next is not None:
print(" -> ", end="")
# moving to the next node
traverseList(head.next)if name == "main": # create a hard-coded linked list: # 10 -> 20 -> 30 -> 40 head = Node(10) head.next = Node(20) head.next.next = Node(30) head.next.next.next = Node(40)
traverseList(head)C#
using System;
// a linked list node class Node { public int Data { get; set; } public Node Next { get; set; }
// constructor to initialize a new node with data
public Node(int newData) {
Data = newData;
Next = null;
}}
class GfG {
// function to traverse and print the singly linked list
static void traverseList(Node head) {
// base condition is when the head is null
if (head == null) {
Console.WriteLine();
return;
}
// printing the current node data
Console.Write(head.Data);
// print arrow if not the last node
if (head.Next != null) {
Console.Write(" -> ");
}
// moving to the next node
traverseList(head.Next);
}
static void Main() {
// create a hard-coded linked list:
// 10 -> 20 -> 30 -> 40
Node head = new Node(10);
head.Next = new Node(20);
head.Next.Next = new Node(30);
head.Next.Next.Next = new Node(40);
traverseList(head);
}}
JavaScript
// a linked list node class Node {
// constructor to initialize a new node with data
constructor(new_data) {
this.data = new_data;
this.next = null;
}}
// function to traverse and print the singly linked list function traverseList(head) {
// base condition is when the head is null
if (head === null) {
console.log();
return;
}
// printing the current node data
process.stdout.write(head.data.toString());
// print arrow if not the last node
if (head.next !== null) {
process.stdout.write(" -> ");
}
// moving to the next node
traverseList(head.next);}
// Driver code
// create a hard-coded linked list: // 10 -> 20 -> 30 -> 40 let head = new Node(10); head.next = new Node(20); head.next.next = new Node(30); head.next.next.next = new Node(40);
// Example of traversing the node and printing traverseList(head);
`
**Time Complexity: O(n), where nis number of nodes in the linked list.
**Auxiliary Space: O(n) because of recursive stack space.

