如何以相反的顺序返回所有链接列表整数的字符串?

时间:2019-03-07 02:24:10

标签: java string debugging linked-list reverse

假定初始curr_node始终为head。 LLNode的实现遵循此代码段的实现。我该如何运作?

private String toString(LLNode<Integer> curr_node) {
    // TODO
    if(curr_node==null)
    {
        return "";
    }
    else
    {
        return curr_node.data+toString(curr_node.link);
    }
}

public class LLNode<T> {
    public T data;
    public LLNode<T> link;
    public LLNode() {
        this(null, null);
    }
    public LLNode(T data, LLNode<T> link) {
        this.data = data;
        this.link = link;
    }
}

1 个答案:

答案 0 :(得分:0)

请参阅:

private String toString(LLNode<Integer> curr_node) {
    if(curr_node==null)
    {
        return "";
    }
    else
    {
        return toString(curr_node.link) + "," + curr_node.data; // Your code is the reverse
    }
}