Skip to content

Commit 79b631f

Browse files
committed
Add URL for GeeksForGeeks explanation and test cases for helper functions
1 parent 4dc37cb commit 79b631f

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

data_structures/linked_list/partition_linked_list.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
nodes greater than or equal to x. The original relative order of the nodes in each
44
partition should be preserved. The partition value x can appear anywhere in the "right
55
partition" 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

810
from __future__ import annotations
@@ -17,7 +19,19 @@ class ListNode:
1719

1820

1921
def 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

3145
def 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

Comments
 (0)