We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
It works like this: in a double-linked list that has been reversed, the values of each node's 'next' and 'prev' fields are swapped (the nodes before and after each node swap positions). The code above does just that; starting at the head of the list it works through the list swapping 'next' and 'prev' values.
As for why you got a NullPointer exception, a pointer without a target has a value of NULL, thus a loop that moves from node to node ends when its "iterator" variable is assigned null (that's why newHead's value is updated before temp's value). In short, your print statement tried to get a field value from said null.
Reverse a doubly linked list
You are viewing a single comment's thread. Return to all comments →
It works like this: in a double-linked list that has been reversed, the values of each node's 'next' and 'prev' fields are swapped (the nodes before and after each node swap positions). The code above does just that; starting at the head of the list it works through the list swapping 'next' and 'prev' values.
As for why you got a NullPointer exception, a pointer without a target has a value of NULL, thus a loop that moves from node to node ends when its "iterator" variable is assigned null (that's why newHead's value is updated before temp's value). In short, your print statement tried to get a field value from said null.
Here's a four line solution using recursion.
Good Logic! It helped alot! Thank You!
beautiful!
Neat solution. Loved it !
Brilliant
hey, can you explain 3,4 line. i don't understand, why you use this.
you solution is the best
here is problem solution in java python c++ c and javascript programming.
HackerRank Reverse a doubly linked list solution