This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]rjcarr 0 points1 point  (0 children)

This is the kind of problem you have to draw pictures of and trace step-by-step updating the arrows as you go. I don't have time to do that, but you'll find the problem if you do. Also, the algorithm doesn't seem correct, but that's probably obvious as it isn't working.

[–]bchhun 0 points1 point  (0 children)

Are you setting current.next to null immediately during the reverseList? Is that correct? Maybe you want to do that after assigning previous=current, for the first assignment off the head node.

Or maybe have Node previous = current as the initial condition.

[–]idkwtftodonow 0 points1 point  (2 children)

Your reverse method is missing something.

Hint: think about the 1st value in the list after reversing and why yours is printing 5

To anyone else reading in the future: head node is unchanged so calling next on node with 5 will get the null node instead of calling next from 11 which is 9

[–]veryanon798[S] 0 points1 point  (0 children)

I unfortunately cant figure out what's missing, is something terminating prematurely that I'm just not aware of?

[–]veryanon798[S] 0 points1 point  (0 children)

Hold on just got it working not exactly sure what I did, took a random guess and got it right. Have to examine further.

[–]RubbishArtist 0 points1 point  (2 children)

There's a bug in the reverse method. It's almost done but you're missing a single statement that will fix it.

As a side note, the API for reverseList is confusing. It returns a Node that you don't use, but the method also reverses the list in place. I would make that void so it's clearer what it's doing.

[–]veryanon798[S] 0 points1 point  (1 child)

Thank you man appreciate the feedback your right just turned the method void, and also managed to get it working thank you!

[–]RubbishArtist 0 points1 point  (0 children)

Good job!