mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Merge pull request #60 from donnemartin/develop
Polish kth to last element challenge and solution
This commit is contained in:
commit
1e9ffd3f33
|
@ -175,21 +175,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.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
|
@ -63,10 +63,10 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Algorithm\n",
|
"## Algorithm\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Setup two pointers, current and previous\n",
|
"* Setup two pointers, fast and slow\n",
|
||||||
"* Give current a headstart, incrementing it once if k = 1, twice if k = 2, ...\n",
|
"* Give fast a headstart, incrementing it once if k = 1, twice if k = 2, ...\n",
|
||||||
"* Increment both pointers until current reaches the end\n",
|
"* Increment both pointers until fast reaches the end\n",
|
||||||
"* Return the value of previous\n",
|
"* Return the value of slow\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
"* Time: O(n)\n",
|
"* Time: O(n)\n",
|
||||||
|
@ -103,26 +103,23 @@
|
||||||
"\n",
|
"\n",
|
||||||
" def kth_to_last_elem(self, k):\n",
|
" def kth_to_last_elem(self, k):\n",
|
||||||
" if self.head is None:\n",
|
" if self.head is None:\n",
|
||||||
" return\n",
|
" return None\n",
|
||||||
" if k >= len(self):\n",
|
" fast = self.head\n",
|
||||||
" return\n",
|
" slow = self.head\n",
|
||||||
" curr = self.head\n",
|
|
||||||
" prev = self.head\n",
|
|
||||||
" counter = 0\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
" # Give current a headstart, incrementing it\n",
|
" # Give fast a headstart, incrementing it\n",
|
||||||
" # once for k = 1, twice for k = 2, etc\n",
|
" # once for k = 1, twice for k = 2, etc\n",
|
||||||
" while counter < k:\n",
|
" for _ in range(k):\n",
|
||||||
" curr = curr.next\n",
|
" fast = fast.next\n",
|
||||||
" counter += 1\n",
|
" # If k >= num elements, return None\n",
|
||||||
" if curr is None:\n",
|
" if fast is None:\n",
|
||||||
" return\n",
|
" return None\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # Increment both pointers until current reaches the end\n",
|
" # Increment both pointers until fast reaches the end\n",
|
||||||
" while curr.next is not None:\n",
|
" while fast.next is not None:\n",
|
||||||
" curr = curr.next\n",
|
" fast = fast.next\n",
|
||||||
" prev = prev.next\n",
|
" slow = slow.next\n",
|
||||||
" return prev.data"
|
" return slow.data"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -212,21 +209,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.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user