forked from gitgik/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[chore] update readme, replace py file with notebook
- Loading branch information
Showing
3 changed files
with
221 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
{ | ||
"metadata": { | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.0-final" | ||
}, | ||
"orig_nbformat": 2, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3.9.0 64-bit", | ||
"metadata": { | ||
"interpreter": { | ||
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" | ||
} | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2, | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"class Node:\n", | ||
" def __init__(self, value):\n", | ||
" self.value = value\n", | ||
" self.prev = None\n", | ||
" self.next = None\n", | ||
"\n", | ||
"\n", | ||
"class DoubleLinkedList:\n", | ||
" def __init__(self):\n", | ||
" self.head = None\n", | ||
" self.tail = None\n", | ||
"\n", | ||
" def set_head(self, node):\n", | ||
" \"\"\"O(1) time | O(1) space\"\"\"\n", | ||
" if self.head is None:\n", | ||
" self.head = node\n", | ||
" self.tail = node\n", | ||
" return\n", | ||
" self.insert_before(self.head, node)\n", | ||
"\n", | ||
" def set_tail(self, node):\n", | ||
" \"\"\"O(1) time | O(1) space\"\"\"\n", | ||
" if self.tail is None:\n", | ||
" self.set_head(node)\n", | ||
" return\n", | ||
" self.insert_after(self.tail, node)\n", | ||
"\n", | ||
" def insert_before(self, node, node_to_insert):\n", | ||
" \"\"\"O(1) time | O(1) space\"\"\"\n", | ||
" if node_to_insert == self.head and node_to_insert == self.tail:\n", | ||
" return\n", | ||
" # first, remove node if it exists\n", | ||
" self.remove(node_to_insert)\n", | ||
" node_to_insert.prev = node.prev\n", | ||
" node_to_insert.next = node\n", | ||
"\n", | ||
" if node.prev is None:\n", | ||
" self.head = node_to_insert\n", | ||
" else:\n", | ||
" node.prev.next = node_to_insert\n", | ||
" node.prev = node_to_insert\n", | ||
"\n", | ||
" def insert_after(self, node, node_to_insert):\n", | ||
" \"\"\"O(1) time | O(1) space\"\"\"\n", | ||
" if node_to_insert == self.head and node_to_insert == self.tail:\n", | ||
" return\n", | ||
" # first remove the node to insert if it currently exists in the linked list\n", | ||
" self.remove(node_to_insert)\n", | ||
" node_to_insert.prev = node\n", | ||
" node_to_insert.next = node.next\n", | ||
"\n", | ||
" if node.next is None:\n", | ||
" self.tail = node_to_insert\n", | ||
" else:\n", | ||
" node.prev.next = node_to_insert\n", | ||
" node.next = node_to_insert\n", | ||
"\n", | ||
" def insert_at_position(self, position, node_to_insert):\n", | ||
" \"\"\"O(1) space | O(p) time - since we are iterating up until position p\"\"\"\n", | ||
" if position == 1:\n", | ||
" self.set_head(node_to_insert)\n", | ||
" return\n", | ||
" node = self.head\n", | ||
" current_position = 1\n", | ||
" while node is not None and current_position != position:\n", | ||
" node = node.next\n", | ||
" current_position += 1\n", | ||
" if node is not None:\n", | ||
" self.insert_before(node, node_to_insert)\n", | ||
" else:\n", | ||
" # node is None so we are at the tail\n", | ||
" self.set_tail(node_to_insert)\n", | ||
"\n", | ||
" def remove_nodes_with_value(self, value):\n", | ||
" \"\"\"O(n) time | O(1) space\"\"\"\n", | ||
" node = self.head\n", | ||
" while node is not None:\n", | ||
" # temporary save the current node, to not lose it while we check\n", | ||
" # if it needs to be removed\n", | ||
" node_to_remove = node\n", | ||
" # update the node to be the next node\n", | ||
" node = node.next\n", | ||
" if node_to_remove.value == value:\n", | ||
" self.remove(node_to_remove)\n", | ||
"\n", | ||
" def remove(self, node):\n", | ||
" \"\"\"O(1) time | O(1) space\"\"\"\n", | ||
" if node == self.head:\n", | ||
" self.head = self.head.next\n", | ||
" if node == self.tail:\n", | ||
" self.tail = self.tail.prev\n", | ||
" self.update_node_bindings(node)\n", | ||
"\n", | ||
" def contains_node_with_value(self, value):\n", | ||
" \"\"\"O(n) time | O(1) space\"\"\"\n", | ||
" node = self.head\n", | ||
" while node is not None and node.value != value:\n", | ||
" node = node.next\n", | ||
" return node is not None\n", | ||
"\n", | ||
" def update_node_bindings(self, node):\n", | ||
" \"\"\"Update the pointers of a node when a node is removed from the doubly linked list.\n", | ||
" \"\"\"\n", | ||
" # the order in which you remove pointers matters. otherwise, we might delete nodes\n", | ||
" # and never recover them forever.\n", | ||
" if node.prev is not None:\n", | ||
" node.prev.next = node.next\n", | ||
" if node.next is not None:\n", | ||
" node.next.prev = node.prev\n", | ||
" node.prev = None\n", | ||
" node.next = None" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"\n", | ||
"doubly_linked_list = DoubleLinkedList()\n", | ||
"first_node = Node(1)\n", | ||
"second_node = Node(2)\n", | ||
"third_node = Node(3)\n", | ||
"\n", | ||
"doubly_linked_list.set_head(first_node)\n", | ||
"doubly_linked_list.set_tail(third_node)\n", | ||
"doubly_linked_list.insert_before(third_node, second_node)\n", | ||
"another_node = Node(7)\n", | ||
"doubly_linked_list.insert_after(third_node, another_node)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"output_type": "execute_result", | ||
"data": { | ||
"text/plain": [ | ||
"1" | ||
] | ||
}, | ||
"metadata": {}, | ||
"execution_count": 5 | ||
} | ||
], | ||
"source": [ | ||
"doubly_linked_list.head.value" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
] | ||
} |
This file was deleted.
Oops, something went wrong.