public static void main(String[] args) {

    LinkedList list = new LinkedList();
    list.insertFront(1);
    list.insertFront(2);
    list.insertFront(3);
    System.out.println(list.toString());
}

String toString() {
            String result = "";
            LinkedListNode current = head;
            while(current.getNext() != null){
                result += current.getData();
                if(current.getNext() != null){
                     result += ", ";
                }
                current = current.getNext();
            }
            return "List: " + result;
}
Answer from Marin on Stack Overflow
Top answer
1 of 6
7
public static void main(String[] args) {

    LinkedList list = new LinkedList();
    list.insertFront(1);
    list.insertFront(2);
    list.insertFront(3);
    System.out.println(list.toString());
}

String toString() {
            String result = "";
            LinkedListNode current = head;
            while(current.getNext() != null){
                result += current.getData();
                if(current.getNext() != null){
                     result += ", ";
                }
                current = current.getNext();
            }
            return "List: " + result;
}
2 of 6
5

As has been pointed out in some other answers and comments, what you are missing here is a call to the JVM System class to print out the string generated by your toString() method.

LinkedList myLinkedList = new LinkedList();
System.out.println(myLinkedList.toString());

This will get the job done, but I wouldn't recommend doing it that way. If we take a look at the javadocs for the Object class, we find this description for toString():

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The emphasis added there is my own. You are creating a string that contains the entire state of the linked list, which somebody using your class is probably not expecting. I would recommend the following changes:

  1. Add a toString() method to your LinkedListNode class.
  2. Update the toString() method in your LinkedList class to be more concise.
  3. Add a new method called printList() to your LinkedList class that does what you are currently expecting toString() to do.

In LinkedListNode:

public String toString(){
   return "LinkedListNode with data: " + getData();
}

In LinkedList:

public int size(){
    int currentSize = 0;
    LinkedListNode current = head;
    while(current != null){
        currentSize = currentSize + 1;
        current = current.getNext();
    }

    return currentSize;
}

public String toString(){
    return "LinkedList with " + size() + "elements.";
}

public void printList(){
    System.out.println("Contents of " + toString());

    LinkedListNode current = head;
    while(current != null){
        System.out.println(current.toString());
        current = current.getNext();
    }

}
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › print-linked-list
Print Linked List - GeeksforGeeks
August 6, 2025 - #include <stdio.h> #include <stdlib.h> ... node->next = NULL; return node; } // Function to print the singly linked list void printList(struct Node* head) { // A loop that runs till head is NULL while (head != NULL) { // Printing data ...
Discussions

How do I print a Linked List using a ToString method?
you need to actually return a concatanated string of your contents in your linked list. Loop through and pull out the data that means most to you and add to a string. More on reddit.com
🌐 r/javahelp
12
13
May 9, 2020
Printing contents of nodes in linked lists
Can i see the members of the movie class More on reddit.com
🌐 r/learnjava
12
10
April 23, 2022
Printing elements of a linked list in java - Stack Overflow
I am trying to implement linked list in java. In my main class I get some integers from user and put them in a linked list and then print out my linked list elements. Everything's working fine so f... More on stackoverflow.com
🌐 stackoverflow.com
How do I display the contents of a Linked List in Java? - Stack Overflow
In Java, I created a linked list class and added a bunch of values. But I don't know how to display those values. ... But it printed out some weird values like ADTList@ followed by some jibberish. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java LinkedList toString() Print Example - Java Code Geeks
March 6, 2025 - The given Java program demonstrates how to use the toString() method to print the contents of a LinkedList. It first imports and initializes a LinkedList of type String named names, then adds three elements: “Alice,” “Bob,” and ...
🌐
Delft Stack
delftstack.com › home › howto › java › print linked list in java
How to Print Linked List in Java | Delft Stack
February 2, 2024 - To print a linked list in Java using a loop, we iterate through the nodes of the list, accessing and printing the data of each node.
🌐
Baeldung
baeldung.com › home › java › java list › printing out a linkedlist using tostring()
Printing out a LinkedList Using toString() | Baeldung
April 17, 2025 - It returns a human-readable string representation of the list. The toString() method offers several advantages, which simplifies LinkedList printing without manual iteration:
🌐
Java2Blog
java2blog.com › home › core java › java collections › print linkedlist in java
Print LinkedList in java - Java2Blog
June 25, 2021 - To print out elements in a LinkedList using streams, you only need to use the forEach() after the intermediate operations. This terminal operation accepts a Consumer which is simply a functional interface with a single argument and returns no result.
🌐
Reddit
reddit.com › r/javahelp › how do i print a linked list using a tostring method?
r/javahelp on Reddit: How do I print a Linked List using a ToString method?
May 9, 2020 -

Hello,

I'm trying to learn LinkedLists but I can't figure out how to print it using the ToString method.

import java.util.LinkedList;

public class MyString {
    private LinkedList<Character> list;

    public MyString(){ //constructor
        this.list = list;
    }

    public void append(char c){ //add char to list
        list.add(c);
    }

    public int indexOf(char c){ //return the index of a char
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) == c) {
                return i;
            }
        }
        return -1;
    }

    public char charAt(int index) throws IndexOutOfBoundsException{ //return the char at an index
        return list.get(index);        
    }

    public String toString(){ //print list
        return list;
    }

    public static void main(String[] args) {
        
    }
}

I googled around but didn't quite understand the concept.

Currently the error I'm getting for my ToString method is: Type mismatch: cannot convert from LinkedList<Character> to String

Any help is appreciated :)

Find elsewhere
🌐
Reddit
reddit.com › r/learnjava › printing contents of nodes in linked lists
r/learnjava on Reddit: Printing contents of nodes in linked lists
April 23, 2022 -

Hey y'all. Learning about Linked Lists in my class at the moment, and my professor wasn't able to teach us much about linked lists (family issues). He's unavailable until the end of the semester, but with that being two weeks away, I'm in a bind.

Tasked with an assignment that creates a linked list of movies. The 'movie' class creates objects with 5 attributes along with an override of the toString method. In the actual linked lists class, there is a show_all() method that's called in the main method - it utilizes the toString method in order to print a label for the movie objects. However, my show_all() method isn't displaying the label when called on. Here's the method:

public static class BlueRayDisk{
        public String title, director;
        public int yearOfRelease;
        public double cost;

        public BlueRayDisk(String t, String d, int y, double c){
            this.title = t;
            this.director = d;
            this.yearOfRelease = y;
            this.cost = c;
        }

        public String toString(){
            return "$" + cost + " " + yearOfRelease + " " + title + ", " + director;
        }
    }

public static class Node{
        public BlueRayDisk disk;
        public Node next;
    }

    public static class BlueRayCollection{
        private Node head = null;

        public void add(String title, String director, Integer year, Double cost){
             Node tempNode = new Node();
             tempNode.disk = new BlueRayDisk(title,director,year,cost);

             if (head == null)
             {
                 head = tempNode;
             }
             else{
                 Node current = head;

                 while(current.next!= null){
                     current = current.next;

                 }
                 current.next = tempNode;

             }


        }

        public void show_all(){
            Node current = head;
           

            while(current.next != null){
                System.out.println(current.disk);
                current = current.next;
            }

        }

    }

public static void main (String[] args){
        BlueRayCollection a = new BlueRayCollection();
        int user;
        String titl,dir;
        Integer year;
        Double cost;
        Scanner scan = new Scanner(System.in);
        do{
            System.out.println("0. Quit\n" +
                    "1. Add BlueRay to collection\n" +
                    "2. See collection");

            user = scan.nextInt();

            switch(user){
                case(0):
                    break;
                case(1):
                    System.out.println("What is the title?");
                    titl = scan.next();
                    System.out.println("What is the director?");
                    dir = scan.nextLine();
                    scan.nextLine();
                    System.out.println("What is the year of release?");
                    year = scan.nextInt();
                    System.out.println("What is the cost?");
                    cost = scan.nextDouble();

                    a.add(titl,dir,year,cost);
                    break;
                case(2):
                    a.show_all();
                    break;

            }

        }while (user != 0);
    }

Am I missing something? I am struggling with the concept of linked lists a little, but I was able to get my add method to work.

EDIT: Added the LinkedList class (named BlueRayCollection) for clarification along with the node class. Main method shows the add/print all methods in action.

🌐
sebhastian
sebhastian.com › how-to-print-linked-list-java
How to print the values of a LinkedList in Java | sebhastian
March 7, 2022 - When you need to display the values, you can print the LinkedList instance using println() method as shown below: ... To print only one of the values stored in the list, you can call the get() method and pass the index where the value is stored ...
🌐
myCompiler
mycompiler.io › view › A1O4jS5zrJ4
Print Linked List (Java) - myCompiler
November 23, 2023 - class Main { // Definition of ListNode class static class ListNode { public int val; public ListNode next; ListNode(int x) { val = x; next = null; } } // Method to print the linked list public static void printLinkedList(ListNode head) { ListNode ...
Published: Nov 23, 2023
🌐
GitHub
gist.github.com › 94f465a338f58dc48944
Print the Elements of a Linked List · GitHub
Print the Elements of a Linked List · Raw · PrintElement.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
GeeksforGeeks
geeksforgeeks.org › java › implementing-a-linked-list-in-java-using-class
Implementing a Linked List in Java using Class - GeeksforGeeks
July 11, 2025 - */ LinkedList list = new LinkedList(); ... list = insert(list, 6); list = insert(list, 7); list = insert(list, 8); // Print the LinkedList printList(list); } } ... Traversal: For traversal, below is a general-purpose function printList() ...
🌐
w3resource
w3resource.com › java-exercises › collection › java-collection-linked-list-exercise-11.php
Java: Display the elements and their positions in a linked list
Write a Java program to iterate over a linked list and print each element along with its position using a loop counter.
🌐
CodeAhoy
codeahoy.com › java › java-linked-list
LinkedList in Java | CodeAhoy
February 21, 2020 - You can also print linked lists with Java 8 Streams using the forEach(...) method:
🌐
Javatpoint
javatpoint.com › java-program-to-create-and-display-a-singly-linked-list
Java Program to create and display a singly linked list - Javatpoint
Java program to print the following pattern on the console 12 · Singly linked list Examples in Java · Java Program to create and display a singly linked list · Java program to create a singly linked list of n nodes and count the number of nodes · Java program to create a singly linked list of n nodes and display it in reverse order ·