mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Added linked list find method.
This commit is contained in:
parent
8c9482c488
commit
f652c7db1e
|
@ -43,11 +43,14 @@
|
|||
"\n",
|
||||
"### Find\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",
|
||||
"### Delete\n",
|
||||
"\n",
|
||||
"* Coming soon\n",
|
||||
"* \n",
|
||||
"\n",
|
||||
"### Print\n",
|
||||
"\n",
|
||||
|
@ -77,7 +80,11 @@
|
|||
"\n",
|
||||
"### Find\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",
|
||||
"Complexity:\n",
|
||||
"* Time: O(n)\n",
|
||||
|
@ -110,7 +117,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -137,6 +144,18 @@
|
|||
" else:\n",
|
||||
" node.next = self.head\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",
|
||||
" def print_list(self):\n",
|
||||
" curr_node = self.head\n",
|
||||
|
@ -147,23 +166,11 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 30,
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"10\n",
|
||||
"10\n",
|
||||
"bc\n",
|
||||
"a\n",
|
||||
"10\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Test insert_to_front\n",
|
||||
"# Insert in an empty list\n",
|
||||
|
@ -181,7 +188,37 @@
|
|||
},
|
||||
{
|
||||
"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": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue
Block a user