简体   繁体   中英

Creating a toString output from a LinkedList

I've been trying to figure out what exactly the LinkedList is called and supposed to be called as within toString2. I am still new to LinkedLists and don't quite understand them very clearly yet. This is what I've figured out, not really sure where to go from here since I'm confused on the LinkedList' name.

public String toString2(){
    String output = "";
    
    while(node != null){
    }

    return output;
}

Where do I get the node from? That's the main thing that I'm confused about.

Original Problem ( Source )

Write a method toString2 that returns a string representation of the list, such as "[5, -2, 9]". Assume that you are adding this method to the LinkedIntList class as defined below:

public class LinkedIntList {
    private ListNode front;   // null for an empty list
    ...
}

Maybe you can try this to iterate and get the values?

  public String toString2(){
    String str = "";
    Node n = first;
    while( n != null ){
        str = str + n.getValue() + " ";
         n = n.getNext();
    }
    return str;

unless you provide complete code it would be hard to know what actual changes need to be made.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM