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.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
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.