mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Polish find loop start solution.
This commit is contained in:
parent
7a166bed67
commit
9c441e9f18
|
@ -60,12 +60,12 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Algorithm\n",
|
"## Algorithm\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Use two pointers i, j, initialized to the head\n",
|
"* Use two references `slow`, `fast`, initialized to the `head`\n",
|
||||||
"* Increment i and j until they meet\n",
|
"* Increment `slow` and `fast` until they meet\n",
|
||||||
" * j is incremented twice as fast as i\n",
|
" * `fast` is incremented twice as fast as `slow`\n",
|
||||||
" * If j's next is None, we do not have a circular list\n",
|
" * If `fast.next` is `None`, we do not have a circular list\n",
|
||||||
"* When i and j meet, move j to the head\n",
|
"* When `slow` and `fast` meet, move `slow` to the `head`\n",
|
||||||
"* Increment i and j one node at a time until they meet\n",
|
"* Increment `slow` and `fast` one node at a time until they meet\n",
|
||||||
"* Where they meet is the start of the loop\n",
|
"* Where they meet is the start of the loop\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
|
@ -103,29 +103,23 @@
|
||||||
"\n",
|
"\n",
|
||||||
" def find_loop_start(self):\n",
|
" def find_loop_start(self):\n",
|
||||||
" if self.head is None or self.head.next is None:\n",
|
" if self.head is None or self.head.next is None:\n",
|
||||||
" return\n",
|
" return None\n",
|
||||||
" i = self.head\n",
|
" slow = self.head\n",
|
||||||
" j = self.head\n",
|
" fast = self.head\n",
|
||||||
" i = i.next\n",
|
" while fast.next is not None:\n",
|
||||||
" j = j.next.next\n",
|
" slow = slow.next\n",
|
||||||
"\n",
|
" fast = fast.next.next\n",
|
||||||
" # Increment i and j until they meet\n",
|
" if fast is None:\n",
|
||||||
" # j is incremented twice as fast as i\n",
|
" return None\n",
|
||||||
" while j != i:\n",
|
" if slow.data == fast.data:\n",
|
||||||
" i = i.next\n",
|
" break\n",
|
||||||
" if j is None or j.next is None:\n",
|
" slow = self.head\n",
|
||||||
" return\n",
|
" while slow.data != fast.data:\n",
|
||||||
" j = j.next.next\n",
|
" slow = slow.next\n",
|
||||||
"\n",
|
" fast = fast.next\n",
|
||||||
" # When i and j meet, move j to the head\n",
|
" if fast is None:\n",
|
||||||
" j = self.head\n",
|
" return None\n",
|
||||||
"\n",
|
" return slow.data"
|
||||||
" # Increment i and j one node at a time until\n",
|
|
||||||
" # they meet, which is the start of the loop\n",
|
|
||||||
" while j != i:\n",
|
|
||||||
" i = i.next\n",
|
|
||||||
" j = j.next\n",
|
|
||||||
" return i.data"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -230,21 +224,21 @@
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python2"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.10"
|
"version": "3.4.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user