o.o
o.o

Reputation: 143

Print Singly linked list in reverse using recursion - Java

Hi I am having a little trouble trying to print a singly linked list in reverse order using recursion. I have looked at some examples but my method doesn't take any parameters. I want to print it out in the following format:

input: [1, 2, 3, 4, 5] and output:[5, 4, 3, 2, 1]

first refers to to first node in my singly linked list and I use StringBuilder to build up my list so I can return it at the end.

This is what I have so far:

public String printReverse() {
    StringBuilder myString = new StringBuilder("[");
    if (head != null) { // base case
        head = head.next;
        myString.append(head.value);   // line 406
        myString.append(", ");         // line 407
        printReverse();                // line 408
    }
    myString = myString.append("]");
    return myString.toString();
}

I get the following error:

Exception in thread "main" java.lang.NullPointerException
    at myprog.SLL$Node.access$100(SLL.java:445)

    at myprog.SLL.printReverse(SLL.java:406)

    at myprog.SLL.printReverse(SLL.java:408)

    at myprog.SLL.printReverse(SLL.java:408)

    at myprog.SLL.printReverse(SLL.java:408)

    at myprog.SLL.printReverse(SLL.java:408)

    at myprog.SLLApp.myMethod(SLLApp.java:198)

    at myprog.SLLApp.<init>(SLLApp.java:37)

    at myprog.SLLApp.main(SLLApp.java:26)

I don't see what I am doing wrong, but I suspect it may be the way I call the method on itself. Can anyone suggest what I may be doing wrong and how I may go about fixing it?

Thanks!

Upvotes: 0

Views: 5371

Answers (3)

Satheesh Kumar
Satheesh Kumar

Reputation: 404

A Javascript solution.

function printReverse(list) {
    if (!list) return;
    if (list.next) {
        printReverse(list.next);  
    }
    console.log(list.value);
}

Upvotes: 0

Bohemian
Bohemian

Reputation: 425318

You are over complicating things. Lets look at the pseudo code:

  • initial node is head
  • if next is null print blank (recursion termination condition)
  • else recurse to next node
  • then print current node

In code, this becomes:

public String printReverse() {
    return printReverse(head); 
}

private String printReverse(Node n) {
    return next == null ? "" : (printReverse(next) + n.value);
}

It's really only two lines of code - see KISS.

Regarding the second private method, it is very common for the public method of a recursive implementation to just set ip the call to a private recursive method with the appropriate initial state.

Upvotes: 2

Keppil
Keppil

Reputation: 46239

You can't declare the result variable inside the method. You can have it as parameter to a private helper method though. Here is a sample implementation:

public String printReverse(Elem elem) {
    return internalReverse(elem, new StringBuilder("[")).append("]").toString();
}
private StringBuilder internalReverse(Elem elem, StringBuilder result) {
    if (elem != null) { // base case
        result.append(internalReverse(elem.next, result));                
        if (result.size() > 1) {
            result.append(", ")
        }            
        result.append(elem);
    }
    return result;
}

Upvotes: 1

Related Questions