33nodes greater than or equal to x. The original relative order of the nodes in each
44partition should be preserved. The partition value x can appear anywhere in the "right
55partition" and it does not need to appear between the left and right partitions.
6+
7+ Explanation from GeeksforGeeks: https://www.geeksforgeeks.org/dsa/partitioning-a-linked-list-around-a-given-value-and-keeping-the-original-order/
68"""
79
810from __future__ import annotations
@@ -17,7 +19,19 @@ class ListNode:
1719
1820
1921def create_linked_list (values : list [int ]) -> ListNode | None :
20- """Helper function to create a linked list from a list of values."""
22+ """
23+ Helper function to create a linked list from a list of values.
24+
25+ >>> head = create_linked_list([1, 2, 3])
26+ >>> head.value
27+ 1
28+ >>> head.next_node.value
29+ 2
30+ >>> head.next_node.next_node.value
31+ 3
32+ >>> create_linked_list([]) is None
33+ True
34+ """
2135 if not values :
2236 return None
2337 head = ListNode (values [0 ])
@@ -29,7 +43,17 @@ def create_linked_list(values: list[int]) -> ListNode | None:
2943
3044
3145def linked_list_to_list (head : ListNode | None ) -> list [int ]:
32- """Helper function to convert a linked list to a list of values."""
46+ """
47+ Helper function to convert a linked list to a list of values.
48+
49+ >>> head = ListNode(1)
50+ >>> head.next_node = ListNode(2)
51+ >>> head.next_node.next_node = ListNode(3)
52+ >>> linked_list_to_list(head)
53+ [1, 2, 3]
54+ >>> linked_list_to_list(None)
55+ []
56+ """
3357 values = []
3458 current = head
3559 while current :
0 commit comments