Polish linked list palindrome challenge and solution

Update constraints and solution code.
This commit is contained in:
Donne Martin 2016-06-18 13:53:04 -04:00
parent 0611967dee
commit 98a71e72e9
2 changed files with 24 additions and 12 deletions

View File

@ -34,9 +34,15 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Can we assume this is a non-circular, singly linked list?\n",
" * Yes\n",
"* Is a single character or number a palindrome?\n", "* Is a single character or number a palindrome?\n",
" * No\n", " * No\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\n",
"* Can we use additional data structures?\n",
" * Yes\n",
"* Can we assume this fits in memory?\n",
" * Yes" " * Yes"
] ]
}, },
@ -182,21 +188,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,

View File

@ -33,9 +33,15 @@
"source": [ "source": [
"## Constraints\n", "## Constraints\n",
"\n", "\n",
"* Can we assume this is a non-circular, singly linked list?\n",
" * Yes\n",
"* Is a single character or number a palindrome?\n", "* Is a single character or number a palindrome?\n",
" * No\n", " * No\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\n",
"* Can we use additional data structures?\n",
" * Yes\n",
"* Can we assume this fits in memory?\n",
" * Yes" " * Yes"
] ]
}, },
@ -119,10 +125,10 @@
"\n", "\n",
" # Compare the reversed list with the original list\n", " # Compare the reversed list with the original list\n",
" # Only need to compare the first half\n", " # Only need to compare the first half\n",
" iterations_to_compare_half = length // 2\n", " iterations = length // 2\n",
" curr = self.head\n", " curr = self.head\n",
" curr_reversed = reversed_list.head\n", " curr_reversed = reversed_list.head\n",
" for _ in range(0, iterations_to_compare_half):\n", " for _ in range(iterations):\n",
" if curr.data != curr_reversed.data:\n", " if curr.data != curr_reversed.data:\n",
" return False\n", " return False\n",
" curr = curr.next\n", " curr = curr.next\n",
@ -229,21 +235,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,