Traversal of Singly Linked List

Last Updated : 17 Aug, 2026

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:

blobid0_1755936039


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:

blobid1_1755936090


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.

Try It Yourself
redirect icon

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 is 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 <iostream>
using namespace std;

// a linked list node
class Node {
public:
    int data;
    Node* next;
    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;
}
C
#include <stdio.h>
#include <stdlib.h>

// structure for a linked list node
struct Node
{
    int data;
    struct Node *next;
};

// function to create a new node
struct Node *createNode(int new_data)
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->data = new_data;
    newNode->next = NULL;
    return newNode;
}

// function to traverse and print the singly linked list
void traverseList(struct Node *head)
{
    while (head != NULL)
    {
        printf("%d", head->data);
        if (head->next != NULL)
            printf(" -> ");
        head = head->next;
    }
    printf("\n");
}

int main()
{
    // create a hard-coded linked list: 10 -> 20 -> 30 -> 40
    struct Node *head = createNode(10);
    head->next = createNode(20);
    head->next->next = createNode(30);
    head->next->next->next = createNode(40);

    traverseList(head);

    return 0;
}
Java
// a linked list node
class Node {
    int data;
    Node next;
    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:
    def __init__(self, new_data):
        self.data = new_data
        self.next = None

# function 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; }
    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(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 n is 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 <iostream>
using namespace std;

// a linked list node
class Node {
public:
    int data;
    Node* next;
    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;
}
C
#include <stdio.h>
#include <stdlib.h>

// structure for a linked list node
struct Node
{
    int data;
    struct Node *next;
};

// function to create a new node
struct Node *createNode(int new_data)
{
    struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));
    newNode->data = new_data;
    newNode->next = NULL;
    return newNode;
}

// function to traverse and print the singly linked list (recursive)
void traverseList(struct Node *head)
{

    // base condition: when the head is NULL
    if (head == NULL)
    {
        printf("\n");
        return;
    }

    // print current node data
    printf("%d", head->data);

    // print arrow if not the last node
    if (head->next != NULL)
    {
        printf(" -> ");
    }

    // recursive call to print next node
    traverseList(head->next);
}

int main()
{
    // create a hard-coded linked list: 10 -> 20 -> 30 -> 40
    struct Node *head = createNode(10);
    head->next = createNode(20);
    head->next->next = createNode(30);
    head->next->next->next = createNode(40);

    traverseList(head);

    return 0;
}
Java
// a linked list node
class Node {
    int data;
    Node next;
    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):
        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; }
    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(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);

Output
10 20 30 40 

Time Complexity: O(n), where n is number of nodes in the linked list.
Auxiliary Space: O(n) because of recursive stack space.

Comment