from linked_list import LinkedList from print_list import print_list_with_forward_arrow def reverse(head): # initialize prev and next pointer to None prev, next = None, None # set current pointer to the head node curr = head # while the current pointer is not None while curr is not None: # set the next pointer to the next node in the list next = curr.next # reverse the current node's pointer to point to the previous node curr.next = prev # set the previous pointer to the current node prev = curr # move the current pointer to the next node curr = next # set the head pointer to the last node, which is the new first node after reversal head = prev # return the new head pointer return head def main(): input = ( [1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6], [3, 2, 1], [10], [1, 2], ) for i in range(len(input)): input_linked_list = LinkedList() input_linked_list.create_linked_list(input[i]) print(i+1, ".\tInput linked list: ", sep="", end="") print_list_with_forward_arrow(input_linked_list.head) print("\n\tReversed linked list: ", end="") print_list_with_forward_arrow(reverse(input_linked_list.head)) print("\n", "-"*100) if __name__ == "__main__": main()