
"""simple docstring"""





class __lowerCAmelCase					:




			"""simple docstring"""







			def __init__(					self    :       Optional[int]							, _snake_case    :       int							, _snake_case    :       Optional[int]=None							, _snake_case    :       Dict=None						)   ->			Union[str, Any]:







								"""simple docstring"""
								A_								=    data
								A_								=    previous
								A_								=    next_node




			def __str__(					self    :       List[str]						)   ->			str:







								"""simple docstring"""
								return F'{self.data}'




			def   lowerCamelCase__  (					self    :       Tuple						)   ->			int:







								"""simple docstring"""
								return self.data




			def   lowerCamelCase__  (					self    :       str						)   ->			Optional[int]:







								"""simple docstring"""
								return self.next






			def   lowerCamelCase__  (					self    :       Union[str, Any]						)   ->			List[Any]:







								"""simple docstring"""
								return self.previous





class __lowerCAmelCase					:




			"""simple docstring"""







			def __init__(					self    :       int							, _snake_case    :       List[Any]						)   ->			Optional[Any]:







								"""simple docstring"""
								A_								=    head




			def __iter__(					self    :       Any						)   ->			Optional[Any]:







								"""simple docstring"""
								return self






			def   lowerCamelCase__  (					self    :       Tuple						)   ->			List[Any]:







								"""simple docstring"""
								if not self.current:
													raise StopIteration
								else:
													A_								=    self.current.get_data()
													A_								=    self.current.get_next()
													return value





class __lowerCAmelCase					:




			"""simple docstring"""







			def __init__(					self    :       str						)   ->			Optional[Any]:







								"""simple docstring"""
								A_								=    None  # First node in list
								A_								=    None  # Last node in list




			def __str__(					self    :       int						)   ->			Any:







								"""simple docstring"""
								A_								=    self.head
								A_								=    []
								while current is not None:
													nodes.append(current.get_data()						)
													A_								=    current.get_next()
								return " ".join(str(_snake_case						) for node in nodes						)




			def __contains__(					self    :       Optional[int]							, _snake_case    :       int						)   ->			Optional[int]:







								"""simple docstring"""
								A_								=    self.head
								while current:
													if current.get_data() == value:
																		return True
													A_								=    current.get_next()
								return False




			def __iter__(					self    :       Any						)   ->			Optional[int]:







								"""simple docstring"""
								return LinkedListIterator(self.head						)




			def   lowerCamelCase__  (					self    :       Any						)   ->			Dict:







								"""simple docstring"""
								if self.head:
													return self.head.get_data()
								return None




			def   lowerCamelCase__  (					self    :       int						)   ->			List[str]:







								"""simple docstring"""
								if self.tail:
													return self.tail.get_data()
								return None




			def   lowerCamelCase__  (					self    :       Tuple							, _snake_case    :       Node						)   ->			None:







								"""simple docstring"""
								if self.head is None:
													A_								=    node
													A_								=    node
								else:
													self.insert_before_node(self.head							, _snake_case						)




			def   lowerCamelCase__  (					self    :       List[Any]							, _snake_case    :       Node						)   ->			None:







								"""simple docstring"""
								if self.head is None:
													self.set_head(_snake_case						)
								else:
													self.insert_after_node(self.tail							, _snake_case						)




			def   lowerCamelCase__  (					self    :       Tuple							, _snake_case    :       int						)   ->			None:







								"""simple docstring"""
								A_								=    Node(_snake_case						)
								if self.head is None:
													self.set_head(_snake_case						)
								else:
													self.set_tail(_snake_case						)




			def   lowerCamelCase__  (					self    :       int							, _snake_case    :       Node							, _snake_case    :       Node						)   ->			None:







								"""simple docstring"""
								A_								=    node
								A_								=    node.previous

								if node.get_previous() is None:
													A_								=    node_to_insert
								else:
													A_								=    node_to_insert

								A_								=    node_to_insert




			def   lowerCamelCase__  (					self    :       str							, _snake_case    :       Node							, _snake_case    :       Node						)   ->			None:







								"""simple docstring"""
								A_								=    node
								A_								=    node.next

								if node.get_next() is None:
													A_								=    node_to_insert
								else:
													A_								=    node_to_insert

								A_								=    node_to_insert




			def   lowerCamelCase__  (					self    :       int							, _snake_case    :       int							, _snake_case    :       int						)   ->			None:







								"""simple docstring"""
								A_								=    1
								A_								=    Node(_snake_case						)
								A_								=    self.head
								while node:
													if current_position == position:
																		self.insert_before_node(_snake_case							, _snake_case						)
																		return
													current_position += 1
													A_								=    node.next
								self.insert_after_node(self.tail							, _snake_case						)




			def   lowerCamelCase__  (					self    :       Union[str, Any]							, _snake_case    :       int						)   ->			Node:







								"""simple docstring"""
								A_								=    self.head
								while node:
													if node.get_data() == item:
																		return node
													A_								=    node.get_next()
								raise Exception("Node not found"						)




			def   lowerCamelCase__  (					self    :       Any							, _snake_case    :       List[Any]						)   ->			Optional[Any]:







								"""simple docstring"""
								if (node := self.get_node(_snake_case						)) is not None:
													if node == self.head:
																		A_								=    self.head.get_next()

													if node == self.tail:
																		A_								=    self.tail.get_previous()

													self.remove_node_pointers(_snake_case						)




			@staticmethod
			def   lowerCamelCase__  (					_snake_case    :       Node						)   ->			None:







								"""simple docstring"""
								if node.get_next():
													A_								=    node.previous

								if node.get_previous():
													A_								=    node.next

								A_								=    None
								A_								=    None






			def   lowerCamelCase__  (					self    :       Any						)   ->			Tuple:







								"""simple docstring"""
								return self.head is None





def 				A_  ():

					'''simple docstring'''







if __name__ == "__main__":
		import doctest

		doctest.testmod()






"""simple docstring"""





import operator
def 				A_  (__a					, __a = False					, __a = None					):

					'''simple docstring'''





					A_								=    operator.lt if reverse else operator.gt
					A_								=    solution or []

					if not arr:
										return solution

					A_								=    [arr.pop(0					)]
					for i, item in enumerate(__a					):
										if _operator(__a					, sublist[-1]					):
															sublist.append(__a					)
															arr.pop(__a					)

    #  merging sublist into solution list
					if not solution:
										solution.extend(__a					)
					else:
										while sublist:
															A_								=    sublist.pop(0					)
															for i, xx in enumerate(__a					):
																				if not _operator(__a					, __a					):
																									solution.insert(__a					, __a					)
																									break
															else:
																				solution.append(__a					)

					strand_sort(__a					, __a					, __a					)
					return solution


if __name__ == "__main__":
		assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5]
		assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]




