Skip to content
Advertisement

Reversing a linked list(Iterative method)

I am writing a code for reversing a linked list in python. The following code does not pass the test case:

JavaScript

while this code passes:

JavaScript

What is the difference between the two?

Advertisement

Answer

In your unrolled code, by the time you get to the last line, curr.next has been overwritten with prev. It no longer has its original value. Both prev and curr will point to the old prev.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement