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.
Things that I like about this: 'while llist' is quite a nice and expressive way of continuing until you hit the terminal None. Returning 'previous_node', defaulted to None, handles the case of an empty llist (single value: None) 'correctly' for the definition.
Things I'm not convinced about: Python should let you swap three variables inline without needing a temporary variable (a, b, c = c, a, b) but expressing things like this feels harder to read (and order is important) versus having a temporary variable and doing things on separate lines.
defreverse(llist):previous_node=Nonewhile(llist):# store next (forward) node link in a temporary namenext_node=llist.next# update node and re-point 'next' backwardsllist.next=previous_node# update the previous node pointer for the next loopprevious_node=llist# advance the 'head' pointer to continuellist=next_nodereturnprevious_node
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Reverse a linked list
You are viewing a single comment's thread. Return to all comments →
Things that I like about this: 'while llist' is quite a nice and expressive way of continuing until you hit the terminal None. Returning 'previous_node', defaulted to None, handles the case of an empty llist (single value: None) 'correctly' for the definition.
Things I'm not convinced about: Python should let you swap three variables inline without needing a temporary variable (a, b, c = c, a, b) but expressing things like this feels harder to read (and order is important) versus having a temporary variable and doing things on separate lines.