def are_palindromes(head_1, head_2): if count_nodes(head_1) != count_nodes(head_2): return False # Find the tail of the second list to begin with current_two = head_2 while current_two.next is not None: current_two = current_two.next tail_2 = current_two # Start comparing the first element of list one with the last element of # list two, and work inwards until we're done or they don't match. current_one, current_two = head_1, tail_2 while current_one.next is not None: if current_one.data != current_two.data: return False current_one = current_one.next next_two = head_2 while next_two.next is not current_two: next_two = next_two.next current_two = next_two return True
pointer2 = pointer2.next pointer1 = pointer1.next # ------------------------------------------------------- for _ in range(10): # make the size of this linked list somewhere between # 5 and 30 size = random.randint(5, 31) # create a list of "size" number of integers and then # add the integers 0-4 to it again as duplicate data ll_values = list(range(size)) ll_values.extend(list(range(5))) # shuffle the list so it's in random order random.shuffle(ll_values) head = get_singly_linked_list_from_values(ll_values) # at this point remove the duplicates remove_duplicates(head) # the count (number of nodes) of the linked list should # be equal to "size" defined above, since duplicates should # be removed assert count_nodes(head) == size