0

I have currently have 2 classes, Images and ImageNode. Inside the ImageNode class I have a recursive method that reverses the linked list. I believe my code is correct for that method, however i'm confused as to how I should be calling this method inside my Images class.

ImageNode method ---

 public ImageNode reverseUsingPrevious(ImageNode previous) {
    if(previous == null) return previous;

    ImageNode next = previous.getNext();

    if(next == null) return previous;

    previous.setNext(null);
    ImageNode rev = reverseUsingPrevious(next);
    next.setNext(previous);
    return rev;
} 

Images method ----

private void reverseRec() {
    cursor.reverseUsingPrevious(head);
//the cursor is the currently selected image(node), head is the start of the linked list
}

I'm not 100% sure what I should be parsing into reverseUsingPrevious method.

5
  • 2
    "I believe my code is correct" So what is your question? What does not work? Commented Sep 19, 2014 at 9:43
  • I believe my reverseUsingPrevious is correct but not my reverseRec() method... that method is causing the problem Commented Sep 19, 2014 at 9:45
  • Please specify: is it a double linked list? Please post the ImageNode's class attributes. Did your task explicitly demand a recursive methods as solution? Commented Sep 19, 2014 at 9:47
  • You need to put a lot more info in this question. I'd suggest you remove all the stuff about images and nodes etc.. and think about what, fundametally, you are having problems with and ask that question. I wont vote you down since you are new, but you are asking for it. Commented Sep 19, 2014 at 10:13
  • My apologies, I will re-evaluate my problem and try explain it a bit better. Thanks Commented Sep 19, 2014 at 10:15

1 Answer 1

1

Update the head by adding head = before the cursor.reverseUsingPrevious(head);.

Extra tip:

Even though the method reverseUsingPrevious probably works as it is, it doesn't call any methods or variables on the local object at all (apart from the call to reverseUsingPrevious), so it could just as well have been static.

But rather than making it static, you could drop the parameter (ImageNode previous), and replace every instance of previous with this.

Now, instead of calling reverseUsingPrevious(node), you put the node in front instead: node.reverseUsingPrevious().

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I knew I was close, thanks for understanding what I was trying to get across. I do believe it could be better but It does the job I need it to do... Thanks for the tip, will keep that in mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.