You are viewing a single comment's thread. Return to all comments →
if not head:
head.next, head.prev = head.prev, head.next
if not head.prev:
will you explain this code?
I like this solution, simple and elegant.
In python you can switch variables in a single line like shown above, so the code goes through each node at a time reversing the connections until you hit the final node.
The last node returned would be the final one encountered, which is now the first element.
I like this, the if not head is only for checking if the list is empty. and if not head.prev is the terminate condition for recursive, am I right?
Hi, yes, you are correct on both counts.