Table of Contents

    Traversal in Linked List in Java — Step by Step Explanation

    In this article, we will learn how to traverse a linked list in Java step by step.

    Traversal means visiting each node of the linked list one by one and processing its data.


    What is Traversal?

    Traversal of a linked list means moving through all the nodes of the list from the first node to the last node.

    During traversal, we usually:

    • Display node values
    • Count nodes
    • Search for an item
    • Process data stored in nodes

    Algorithm to Traverse a Linked List

    1. ptr = start
    2. Repeat until ptr = NULL
    3. Print ptr.INFO
    4. ptr = ptr.LINK
    5. END

    Explanation

    • ptr starts from the first node
    • The data of the current node is printed
    • ptr moves to the next node
    • The process continues until ptr becomes NULL

    Step 1: Understanding the Node Class

    Each node contains:

    • data — stores the value
    • link — stores the reference of the next node
    class Node {
        int data;
        Node link;
    
        Node(int d) {
            data = d;
            link = null;
        }
    }
    
    class TestNode {
        public static void main(String[] args) {
            Node n = new Node(10);
    
            System.out.println("Data: " + n.data);
            System.out.println("Link: " + n.link);
        }
    }

    Explanation

    A node stores data and the address of the next node.

    If the node is the last node, its link becomes null.


    Step 2: Creating a Linked List

    The linked list starts from a reference variable called start.

    class Node {
        int data;
        Node link;
    
        Node(int d) {
            data = d;
            link = null;
        }
    }
    
    class LinkedList {
        Node start;
    
        LinkedList() {
            start = null;
        }
    
        boolean isEmpty() {
            return start == null;
        }
    }
    
    class TestList {
        public static void main(String[] args) {
            LinkedList list = new LinkedList();
    
            if (list.isEmpty()) {
                System.out.println("List is empty");
            }
        }
    }

    Explanation

    If start is null, the list contains no node.


    Step 3: Inserting Nodes into the List

    Before traversal, we need some nodes in the linked list.

    class Node {
        int data;
        Node link;
    
        Node(int d) {
            data = d;
            link = null;
        }
    }
    
    class LinkedList {
        Node start;
    
        void insertAtEnd(int value) {
            Node newNode = new Node(value);
    
            if (start == null) {
                start = newNode;
                return;
            }
    
            Node ptr = start;
    
            while (ptr.link != null) {
                ptr = ptr.link;
            }
    
            ptr.link = newNode;
        }
    
        void display() {
            Node ptr = start;
    
            while (ptr != null) {
                System.out.print(ptr.data + " -> ");
                ptr = ptr.link;
            }
    
            System.out.println("NULL");
        }
    }
    
    class TestInsert {
        public static void main(String[] args) {
            LinkedList list = new LinkedList();
    
            list.insertAtEnd(10);
            list.insertAtEnd(20);
            list.insertAtEnd(30);
    
            list.display();
        }
    }

    Output

    10 -> 20 -> 30 -> NULL

    Step 4: Basic Traversal of a Linked List

    Traversal means moving from one node to another until the last node is reached.

    class Node {
        int data;
        Node link;
    
        Node(int d) {
            data = d;
            link = null;
        }
    }
    
    class LinkedList {
        Node start;
    
        void insertAtEnd(int value) {
            Node newNode = new Node(value);
    
            if (start == null) {
                start = newNode;
                return;
            }
    
            Node ptr = start;
    
            while (ptr.link != null) {
                ptr = ptr.link;
            }
    
            ptr.link = newNode;
        }
    
        void traverse() {
            Node ptr = start;
    
            while (ptr != null) {
                System.out.print(ptr.data + " -> ");
                ptr = ptr.link;
            }
    
            System.out.println("NULL");
        }
    }
    
    class TestTraverse {
        public static void main(String[] args) {
            LinkedList list = new LinkedList();
    
            list.insertAtEnd(5);
            list.insertAtEnd(2);
            list.insertAtEnd(7);
            list.insertAtEnd(4);
    
            list.traverse();
        }
    }

    Output

    5 -> 2 -> 7 -> 4 -> NULL

    Explanation

    Here:

    • ptr starts from the first node
    • Each node’s data is printed
    • ptr moves to the next node using ptr.link
    • The loop stops when ptr becomes null

    Step 5: Traversing with Better Output Formatting

    Now let us print the last node differently.

    class Node {
        int data;
        Node link;
    
        Node(int d) {
            data = d;
            link = null;
        }
    }
    
    class LinkedList {
        Node start;
    
        void insertAtEnd(int value) {
            Node newNode = new Node(value);
    
            if (start == null) {
                start = newNode;
                return;
            }
    
            Node ptr = start;
    
            while (ptr.link != null) {
                ptr = ptr.link;
            }
    
            ptr.link = newNode;
        }
    
        void traverse() {
            Node ptr = start;
    
            while (ptr.link != null) {
                System.out.print(ptr.data + " -> ");
                ptr = ptr.link;
            }
    
            System.out.println(ptr.data + " !!!!");
        }
    }
    
    class TestTraverseStyle {
        public static void main(String[] args) {
            LinkedList list = new LinkedList();
    
            list.insertAtEnd(1);
            list.insertAtEnd(2);
            list.insertAtEnd(3);
            list.insertAtEnd(4);
    
            list.traverse();
        }
    }

    Output

    1 -> 2 -> 3 -> 4 !!!!

    Explanation

    The last node is printed separately to show the end of the linked list clearly.


    Step 6: Complete Traversal Program with User Input

    This complete program creates a linked list using user input and then traverses the list.

    import java.io.*;
    
    class Node {
        int data;
        Node link;
    
        Node(int d) {
            data = d;
            link = null;
        }
    }
    
    class LinkedList {
        Node start;
    
        LinkedList() {
            start = null;
        }
    
        boolean isEmpty() {
            return start == null;
        }
    
        void insertAtEnd(int value) {
            Node newNode = new Node(value);
    
            if (start == null) {
                start = newNode;
                return;
            }
    
            Node ptr = start;
    
            while (ptr.link != null) {
                ptr = ptr.link;
            }
    
            ptr.link = newNode;
        }
    
        void traverse() {
            Node ptr = start;
    
            while (ptr.link != null) {
                System.out.print(ptr.data + " -> ");
                ptr = ptr.link;
            }
    
            System.out.println(ptr.data + " !!!!");
        }
    }
    
    class LinkedListTest {
        public static void main(String[] args) throws IOException {
    
            int num;
    
            LinkedList list = new LinkedList();
    
            BufferedReader br = new BufferedReader(
                    new InputStreamReader(System.in));
    
            System.out.println("Starting List Test for TRAVERSAL");
    
            for (int i = 0; i < 7; i++) {
    
                System.out.print("Enter a number: ");
    
                num = Integer.parseInt(br.readLine());
    
                list.insertAtEnd(num);
            }
    
            System.out.println("\nNow the List is:");
    
            list.traverse();
    
            System.out.println("\nList Test Over");
        }
    }

    Sample Output

    Starting List Test for TRAVERSAL
    
    Enter a number: 6
    Enter a number: 8
    Enter a number: 2
    Enter a number: 4
    Enter a number: 1
    Enter a number: 9
    Enter a number: 8
    
    Now the List is:
    
    1 -> 2 -> 4 -> 6 -> 8 -> 8 -> 9 !!!!
    
    List Test Over

    How Traversal Works

    Suppose the linked list is:

    5 -> 2 -> 7 -> 4 -> NULL

    Traversal works like this:

    Step ptr Points To Printed Value
    1 5 5
    2 2 2
    3 7 7
    4 4 4
    5 NULL Traversal Ends

    Important Concepts Used

    • Node creation
    • Linked list traversal
    • Reference variables
    • Looping through nodes
    • Moving pointer to next node
    • Displaying linked list elements

    Recommended Teaching Sequence

    1. Explain node structure
    2. Explain the role of start
    3. Insert nodes into the list
    4. Create a pointer variable
    5. Move pointer from node to node
    6. Print data during traversal
    7. Stop traversal when pointer becomes NULL
    8. Take input from user and test traversal

    Key Learning

    Traversal is one of the most important operations in a linked list.

    Using traversal, we can:

    • Display all elements
    • Search elements
    • Count nodes
    • Modify data
    • Delete nodes

    Traversal works by moving a pointer node-by-node until the pointer becomes null.