mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-05 08:58:02 +00:00
Added linked list find method.
This commit is contained in:
@@ -43,11 +43,14 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"### Find\n",
|
"### Find\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Coming soon\n",
|
"* Find a NULL\n",
|
||||||
|
"* Find in an empty list\n",
|
||||||
|
"* Find in a list with one element or more matching elements\n",
|
||||||
|
"* Find in a list with no matches\n",
|
||||||
"\n",
|
"\n",
|
||||||
"### Delete\n",
|
"### Delete\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Coming soon\n",
|
"* \n",
|
||||||
"\n",
|
"\n",
|
||||||
"### Print\n",
|
"### Print\n",
|
||||||
"\n",
|
"\n",
|
||||||
@@ -77,7 +80,11 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"### Find\n",
|
"### Find\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Coming soon\n",
|
"* If data we are finding is NULL, return\n",
|
||||||
|
"* If the list is empty, return\n",
|
||||||
|
"* For each node\n",
|
||||||
|
" * If the value is a match, return it\n",
|
||||||
|
" * Else, move on to the next node\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
"* Time: O(n)\n",
|
"* Time: O(n)\n",
|
||||||
@@ -110,7 +117,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 19,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
@@ -137,6 +144,18 @@
|
|||||||
" else:\n",
|
" else:\n",
|
||||||
" node.next = self.head\n",
|
" node.next = self.head\n",
|
||||||
" self.head = node\n",
|
" self.head = node\n",
|
||||||
|
" \n",
|
||||||
|
" def find(self, data):\n",
|
||||||
|
" if data is None:\n",
|
||||||
|
" return\n",
|
||||||
|
" if self.head is None:\n",
|
||||||
|
" return\n",
|
||||||
|
" curr_node = self.head\n",
|
||||||
|
" while curr_node is not None:\n",
|
||||||
|
" if curr_node.data == data:\n",
|
||||||
|
" return curr_node\n",
|
||||||
|
" else:\n",
|
||||||
|
" curr_node = curr_node.next\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def print_list(self):\n",
|
" def print_list(self):\n",
|
||||||
" curr_node = self.head\n",
|
" curr_node = self.head\n",
|
||||||
@@ -147,23 +166,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 30,
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
"outputs": [
|
"outputs": [],
|
||||||
{
|
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"10\n",
|
|
||||||
"10\n",
|
|
||||||
"bc\n",
|
|
||||||
"a\n",
|
|
||||||
"10\n"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"source": [
|
"source": [
|
||||||
"# Test insert_to_front\n",
|
"# Test insert_to_front\n",
|
||||||
"# Insert in an empty list\n",
|
"# Insert in an empty list\n",
|
||||||
@@ -181,7 +188,37 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 31,
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"# Test find\n",
|
||||||
|
"# Find a NULL\n",
|
||||||
|
"linked_list = LinkedList(None)\n",
|
||||||
|
"node = linked_list.find('a')\n",
|
||||||
|
"print(node)\n",
|
||||||
|
"# Find in an empty list\n",
|
||||||
|
"head = Node(10)\n",
|
||||||
|
"linked_list = LinkedList(None)\n",
|
||||||
|
"node = linked_list.find(None)\n",
|
||||||
|
"print(node)\n",
|
||||||
|
"# Find in a list with one element or more matching elements\n",
|
||||||
|
"head = Node(10)\n",
|
||||||
|
"linked_list = LinkedList(head)\n",
|
||||||
|
"linked_list.insert_to_front('a')\n",
|
||||||
|
"linked_list.insert_to_front('bc')\n",
|
||||||
|
"node = linked_list.find('a')\n",
|
||||||
|
"print(node)\n",
|
||||||
|
"# Find in a list with no matches\n",
|
||||||
|
"node = linked_list.find('aaa')\n",
|
||||||
|
"print(node)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"collapsed": false
|
"collapsed": false
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user