Skip to content

Commit 09637c7

Browse files
committed
feat: add more problems
1 parent 510af8f commit 09637c7

File tree

4 files changed

+39
-44
lines changed

4 files changed

+39
-44
lines changed

.templates/leetcode/json/linked_list_cycle.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@
3434
"test_helper_methods": [
3535
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" },
3636
{
37-
"name": "_create_cycle_list",
37+
"name": "create_cycle_list",
3838
"parameters": "values: list[int], pos: int",
39-
"body": "if not values:\n return None\n\nhead = ListNode.from_list(values)\nif pos == -1:\n return head\n\n# Find the node at position pos\ncurrent = head\nfor _ in range(pos):\n current = current.next\ncycle_node = current\n\n# Find the tail\ntail = head\nwhile tail.next:\n tail = tail.next\n\n# Create cycle\ntail.next = cycle_node\nreturn head"
39+
"body": "if not values:\n return None\n\nnodes = []\nhead = ListNode(values[0])\nnodes.append(head)\ncurrent = head\n\nfor i in range(1, len(values)):\n current.next = ListNode(values[i])\n current = current.next\n nodes.append(current)\n\nif pos != -1 and pos < len(nodes):\n current.next = nodes[pos]\n\nreturn head"
4040
}
4141
],
4242
"test_methods": [
4343
{
4444
"name": "test_has_cycle",
4545
"parametrize": "values, pos, expected",
4646
"parametrize_typed": "values: list[int], pos: int, expected: bool",
47-
"test_cases": "[([3, 2, 0, -4], 1, True), ([1, 2], 0, True), ([1], -1, False), ([], -1, False), ([1, 2, 3, 4, 5], 2, True), ([1, 2, 3], -1, False)]",
48-
"body": "head = self._create_cycle_list(values, pos)\nresult = self.solution.has_cycle(head)\nassert result == expected"
47+
"test_cases": "[([3, 2, 0, -4], 1, True), ([1, 2], 0, True), ([1], -1, False), ([], -1, False), ([1, 2, 3], -1, False), ([1, 2, 3, 4, 5], 0, True), ([1, 2, 3, 4, 5], 2, True), ([1, 2, 3, 4, 5], 4, True), ([1], 0, True), ([1, 2], 1, True), ([1, 2, 3, 4, 5, 6, 7, 8], 3, True), ([1, 2, 3, 4, 5, 6, 7, 8], -1, False), ([1, 2], -1, False), ([5, 10], 0, True), ([5, 10], 1, True), ([0], -1, False), ([-1, -2, -3], 1, True), ([100, 200, 300], 0, True)]",
48+
"body": "head = self.create_cycle_list(values, pos)\nresult = self.solution.has_cycle(head)\nassert result == expected"
4949
}
5050
],
51-
"playground_imports": "from leetcode_py import ListNode\nfrom solution import Solution",
52-
"playground_test_case": "# Example test case\nvalues = [3, 2, 0, -4]\npos = 1\nexpected = True",
53-
"playground_execution": "head = create_cycle_list(values, pos)\nresult = Solution().has_cycle(head)\nresult",
51+
"playground_imports": "from solution import Solution",
52+
"playground_test_case": "import os\nimport sys\nsys.path.append(os.path.join(os.getcwd(), \\\"..\\\"))\nfrom linked_list_cycle.tests import TestLinkedListCycle\n\n# Example test case\nvalues = [3, 2, 0, -4]\npos = 1\nexpected = True",
53+
"playground_execution": "head = TestLinkedListCycle().create_cycle_list(values, pos)\nresult = Solution().has_cycle(head)\nresult",
5454
"playground_assertion": "assert result == expected"
5555
}

leetcode/linked_list_cycle/playground.ipynb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
"metadata": {},
88
"outputs": [],
99
"source": [
10-
"import os\n",
11-
"import sys\n",
12-
"\n",
13-
"sys.path.append(os.path.join(os.getcwd(), \"..\"))\n",
14-
"\n",
15-
"from linked_list_cycle.tests import TestLinkedListCycle\n",
1610
"from solution import Solution"
1711
]
1812
},
@@ -23,6 +17,12 @@
2317
"metadata": {},
2418
"outputs": [],
2519
"source": [
20+
"import os\n",
21+
"import sys\n",
22+
"\n",
23+
"sys.path.append(os.path.join(os.getcwd(), \"..\"))\n",
24+
"from linked_list_cycle.tests import TestLinkedListCycle\n",
25+
"\n",
2626
"# Example test case\n",
2727
"values = [3, 2, 0, -4]\n",
2828
"pos = 1\n",

leetcode/linked_list_cycle/solution.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ class Solution:
55
# Time: O(n)
66
# Space: O(1)
77
def has_cycle(self, head: ListNode[int] | None) -> bool:
8-
slow = head
98
fast = head
9+
slow = head
1010

11-
while fast is not None and fast.next is not None:
12-
slow = slow.next # type: ignore[union-attr]
11+
while fast and fast.next:
12+
assert slow is not None
1313
fast = fast.next.next
14-
if slow is fast:
15-
return True # Cycle detected
14+
slow = slow.next
15+
if fast is slow:
16+
return True
1617

17-
return False # No cycle
18+
return False

leetcode/linked_list_cycle/tests.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class TestLinkedListCycle:
1010
def setup_method(self):
1111
self.solution = Solution()
1212

13-
def create_cycle_list(self, values: list[int], pos: int) -> ListNode[int] | None:
13+
def create_cycle_list(self, values: list[int], pos: int):
1414
if not values:
1515
return None
1616

@@ -32,30 +32,24 @@ def create_cycle_list(self, values: list[int], pos: int) -> ListNode[int] | None
3232
@pytest.mark.parametrize(
3333
"values, pos, expected",
3434
[
35-
# Basic cases
36-
([3, 2, 0, -4], 1, True), # Cycle to middle
37-
([1, 2], 0, True), # Cycle to head
38-
([1], -1, False), # Single node, no cycle
39-
([], -1, False), # Empty list
40-
([1, 2, 3], -1, False), # Linear list
41-
# Various cycle positions
42-
([1, 2, 3, 4, 5], 0, True), # Cycle to head
43-
([1, 2, 3, 4, 5], 2, True), # Cycle to middle
44-
([1, 2, 3, 4, 5], 4, True), # Cycle to tail
45-
# Self-loop cases
46-
([1], 0, True), # Single node self-loop
47-
([1, 2], 1, True), # Last node self-loop
48-
# Longer lists
49-
([1, 2, 3, 4, 5, 6, 7, 8], 3, True), # Long list with cycle
50-
([1, 2, 3, 4, 5, 6, 7, 8], -1, False), # Long list without cycle
51-
# Two-node cases
52-
([1, 2], -1, False), # Two nodes, no cycle
53-
([5, 10], 0, True), # Two nodes, cycle to first
54-
([5, 10], 1, True), # Two nodes, self-loop on second
55-
# Edge case values
56-
([0], -1, False), # Zero value
57-
([-1, -2, -3], 1, True), # Negative values
58-
([100, 200, 300], 0, True), # Large values
35+
([3, 2, 0, -4], 1, True),
36+
([1, 2], 0, True),
37+
([1], -1, False),
38+
([], -1, False),
39+
([1, 2, 3], -1, False),
40+
([1, 2, 3, 4, 5], 0, True),
41+
([1, 2, 3, 4, 5], 2, True),
42+
([1, 2, 3, 4, 5], 4, True),
43+
([1], 0, True),
44+
([1, 2], 1, True),
45+
([1, 2, 3, 4, 5, 6, 7, 8], 3, True),
46+
([1, 2, 3, 4, 5, 6, 7, 8], -1, False),
47+
([1, 2], -1, False),
48+
([5, 10], 0, True),
49+
([5, 10], 1, True),
50+
([0], -1, False),
51+
([-1, -2, -3], 1, True),
52+
([100, 200, 300], 0, True),
5953
],
6054
)
6155
@logged_test

0 commit comments

Comments
 (0)