简体   繁体   中英

Reverse linked list AND each string in each node itself

I'm all out of ideas. So I have a linked list where each node itself is a string, and I want to return a new Ob object that has it reversed. So for example if the original had two nodes, "abcd" and "def", the result would be "fed" and "dcba" (as opposed to "def" and "abcd"). myFirst is a variable that I define/initialize earlier and refers to the head. I think I may have some type problem, as it's telling my that instead of [feddcba] I'm getting [Ob$Node@4465dade] ... something to do with memory I reckon? Since Ob implements Obj, I know the problem isn't with returning an Ob object as opposed to Obj (in fact it's in the guidelines to return a newly created Ob object).

public Obj reverse() {
    Node before = null;
    Node tmp = myFirst;
    while (tmp != null) {
        StringBuilder copy = new StringBuilder(tmp.info);
        copy.reverse();
        String str = new String(copy.toString());
        tmp.info = str;
        Node next = tmp.next;
        tmp.next = before;
        before = tmp;
        tmp = next;
    }
    Ob ret = new Ob(before.toString()); //note Ob implements Obj
    return ret; 
}

Any help is highly appreciated!

In java, printing an object prints the object's hash code, Ob$Node@4465dade. I think you will want to call the object's toString method to get the value, feddcba.

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