mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Update linked list find loop to return a node (#106)
This commit is contained in:
parent
c961a279fc
commit
bd89e83b65
|
@ -38,6 +38,8 @@
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* Can we assume we are always passed a circular linked list?\n",
|
"* Can we assume we are always passed a circular linked list?\n",
|
||||||
" * No\n",
|
" * No\n",
|
||||||
|
"* When we find a loop, do we return the node or the node's data?\n",
|
||||||
|
" * The node\n",
|
||||||
"* Can we assume we already have a linked list class that can be used for this problem?\n",
|
"* Can we assume we already have a linked list class that can be used for this problem?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
|
@ -160,7 +162,7 @@
|
||||||
" node0 = Node(0, node1)\n",
|
" node0 = Node(0, node1)\n",
|
||||||
" node10.next = node3\n",
|
" node10.next = node3\n",
|
||||||
" linked_list = MyLinkedList(node0)\n",
|
" linked_list = MyLinkedList(node0)\n",
|
||||||
" assert_equal(linked_list.find_loop_start(), 3)\n",
|
" assert_equal(linked_list.find_loop_start(), node3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_find_loop_start')\n",
|
" print('Success: test_find_loop_start')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
|
|
@ -37,6 +37,8 @@
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* Can we assume we are always passed a circular linked list?\n",
|
"* Can we assume we are always passed a circular linked list?\n",
|
||||||
" * No\n",
|
" * No\n",
|
||||||
|
"* When we find a loop, do we return the node or the node's data?\n",
|
||||||
|
" * The node\n",
|
||||||
"* Can we assume we already have a linked list class that can be used for this problem?\n",
|
"* Can we assume we already have a linked list class that can be used for this problem?\n",
|
||||||
" * Yes"
|
" * Yes"
|
||||||
]
|
]
|
||||||
|
@ -119,7 +121,7 @@
|
||||||
" fast = fast.next\n",
|
" fast = fast.next\n",
|
||||||
" if fast is None:\n",
|
" if fast is None:\n",
|
||||||
" return None\n",
|
" return None\n",
|
||||||
" return slow.data"
|
" return slow"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -183,7 +185,7 @@
|
||||||
" node0 = Node(0, node1)\n",
|
" node0 = Node(0, node1)\n",
|
||||||
" node10.next = node3\n",
|
" node10.next = node3\n",
|
||||||
" linked_list = MyLinkedList(node0)\n",
|
" linked_list = MyLinkedList(node0)\n",
|
||||||
" assert_equal(linked_list.find_loop_start(), 3)\n",
|
" assert_equal(linked_list.find_loop_start(), node3)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" print('Success: test_find_loop_start')\n",
|
" print('Success: test_find_loop_start')\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
@ -201,7 +203,8 @@
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 4,
|
"execution_count": 4,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false,
|
||||||
|
"scrolled": true
|
||||||
},
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
|
|
|
@ -35,7 +35,7 @@ class TestFindLoopStart(object):
|
||||||
node0 = Node(0, node1)
|
node0 = Node(0, node1)
|
||||||
node10.next = node3
|
node10.next = node3
|
||||||
linked_list = MyLinkedList(node0)
|
linked_list = MyLinkedList(node0)
|
||||||
assert_equal(linked_list.find_loop_start(), 3)
|
assert_equal(linked_list.find_loop_start(), node3)
|
||||||
|
|
||||||
print('Success: test_find_loop_start')
|
print('Success: test_find_loop_start')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user