• + 2 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.