Given the head of a doubly linked list, reverse the linked list in place. After reversing, the head should point to the last node of the original list, and all next and prev pointers should be adjusted accordingly.
classNode:
def __init__(self, val, prev=None, next=None):
self.val = val
self.prev = prev
self.next = next
classSolution:
defreverse(self, head: "Node") ->"Node":
ifnot head:
returnNone current = head
new_head =Nonewhile current:
# Swap prev and next current.prev, current.next = current.next, current.prev
new_head = current
current = (
current.prev
) # Move to the previous node, which is the next node after swappingreturn new_head