You are viewing a single comment's thread. Return to all comments →
def reverse(llist): curr_node = llist head = None while curr_node is not None: pre_node = curr_node.next curr_node.next = head head = curr_node curr_node = pre_node return head # Example process: # Initial list: head = (1) -> 2 -> 3 -> None # Reversed list building process: # # Before Loop: head = (None), curr_node = 1 # While-loop: # Loop-1: pre_node = 2, head = (1) -> None, curr_node: 1 => 2 # Loop-2: pre_node = 3, head = (2) -> 1 -> None, curr_node: 2 => 3 # Loop-3: pre_node = None, head = (3) -> 2 -> 1 -> None, curr_node: 3 => None # # Reversed list: head = (3) -> 2 -> 1 -> None
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 →