Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 182 additions & 1 deletion Linked Lists/B1_Linked_List.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,172 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"# Define Node class\n",
"class Node:\n",
" def __init__(self, value):\n",
" self.value = value\n",
" self.next = None"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"class LinkedList:\n",
" def __init__(self):\n",
" self.head = None\n",
" self.n = 0 # Keep track of length\n",
"\n",
" def __len__(self):\n",
" return self.n\n",
"\n",
" def insert_head(self, value):\n",
" new_node = Node(value)\n",
" new_node.next = self.head\n",
" self.head = new_node\n",
" self.n += 1\n",
"\n",
" def insert_tail(self, value):\n",
" new_node = Node(value)\n",
" if self.head is None:\n",
" self.head = new_node\n",
" else:\n",
" temp = self.head\n",
" while temp.next:\n",
" temp = temp.next\n",
" temp.next = new_node\n",
" self.n += 1\n",
"\n",
" def insert_at_index(self, index, value):\n",
" if index < 0 or index > self.n:\n",
" raise IndexError(\"Index out of range\")\n",
" \n",
" if index == 0:\n",
" self.insert_head(value)\n",
" return\n",
" \n",
" new_node = Node(value)\n",
" temp = self.head\n",
" for _ in range(index - 1):\n",
" temp = temp.next\n",
" new_node.next = temp.next\n",
" temp.next = new_node\n",
" self.n += 1\n",
"\n",
" def delete_node(self, value):\n",
" if self.head is None:\n",
" return\n",
" \n",
" if self.head.value == value:\n",
" self.head = self.head.next\n",
" self.n -= 1\n",
" return\n",
" \n",
" temp = self.head\n",
" while temp.next and temp.next.value != value:\n",
" temp = temp.next\n",
" \n",
" if temp.next:\n",
" temp.next = temp.next.next\n",
" self.n -= 1\n",
"\n",
" def search(self, value):\n",
" temp = self.head\n",
" index = 0\n",
" while temp:\n",
" if temp.value == value:\n",
" return index\n",
" temp = temp.next\n",
" index += 1\n",
" return -1 # Not found\n",
"\n",
" def reverse(self):\n",
" prev = None\n",
" current = self.head\n",
" while current:\n",
" next_node = current.next\n",
" current.next = prev\n",
" prev = current\n",
" current = next_node\n",
" self.head = prev\n",
"\n",
" def display(self):\n",
" temp = self.head\n",
" while temp:\n",
" print(temp.value, end=\" -> \")\n",
" temp = temp.next\n",
" print(\"None\")\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2 -> 4 -> 1 -> 3 -> None\n"
]
}
],
"source": [
"L = LinkedList()\n",
"L.insert_head(1)\n",
"L.insert_head(2)\n",
"L.insert_tail(3)\n",
"L.insert_at_index(1, 4) # Insert 4 at index 1\n",
"L.display() # Expected: 2 -> 4 -> 1 -> 3 -> None"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Index of 3: 3\n",
"2 -> 1 -> 3 -> None\n"
]
}
],
"source": [
"print(\"Index of 3:\", L.search(3)) # Expected: 3\n",
"L.delete_node(4)\n",
"L.display() # Expected: 2 -> 1 -> 3 -> None"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 -> 1 -> 2 -> None\n"
]
}
],
"source": [
"L.reverse()\n",
"L.display()"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -9,8 +176,22 @@
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"name": "python"
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down
Binary file added Linked Lists/Linked List Diagram.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.