Displaying the Linked List

Here we will write code to display or print the Linked List.

As we have discussed the last node's next link to null, we can utilize this to reach the end of the list and display its contents while traversing.

Below is the Java code.

   private void printList() {
        if(head != null){
            LinkedNode temp = head;
            while(temp != null){
                System.out.print(temp.value + " ");
                temp = temp.next;
            }
            System.out.println();
        }else{
            System.out.println("The List is empty, Please add node elements first");
        }
        
        
    }
Here temp = temp.next; traverse the list one by one until it points to null.
Hope you like it. Please comment for doubts.

Happy Learning !!!
SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment