diff --git a/Sorting Algorithms/.idea/.gitignore b/BST/.idea/.gitignore similarity index 100% rename from Sorting Algorithms/.idea/.gitignore rename to BST/.idea/.gitignore diff --git a/Sorting Algorithms/.idea/Sorting Algorithms.iml b/BST/.idea/BST.iml similarity index 100% rename from Sorting Algorithms/.idea/Sorting Algorithms.iml rename to BST/.idea/BST.iml diff --git a/Sorting Algorithms/.idea/inspectionProfiles/profiles_settings.xml b/BST/.idea/inspectionProfiles/profiles_settings.xml similarity index 100% rename from Sorting Algorithms/.idea/inspectionProfiles/profiles_settings.xml rename to BST/.idea/inspectionProfiles/profiles_settings.xml diff --git a/Sorting Algorithms/.idea/misc.xml b/BST/.idea/misc.xml similarity index 100% rename from Sorting Algorithms/.idea/misc.xml rename to BST/.idea/misc.xml diff --git a/BST/.idea/modules.xml b/BST/.idea/modules.xml new file mode 100644 index 00000000..014ee520 --- /dev/null +++ b/BST/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Sorting Algorithms/.idea/vcs.xml b/BST/.idea/vcs.xml similarity index 100% rename from Sorting Algorithms/.idea/vcs.xml rename to BST/.idea/vcs.xml diff --git a/BST/Easy/01_Ceil_in_BST.py b/BST/Easy/01_Ceil_in_BST.py index 559ee759..3b6a61b2 100644 --- a/BST/Easy/01_Ceil_in_BST.py +++ b/BST/Easy/01_Ceil_in_BST.py @@ -1,4 +1,4 @@ -# Problem URL: https://leetcode.com/problems/ceil-in-a-binary-search-tree +# Problem URL: https://www.geeksforgeeks.org/problems/implementing-ceil-in-bst/1 # TODO: Implement the solution def solution(): diff --git a/BST/Medium/236. Lowest Common Ancestor of a Binary Tree.py b/BST/Medium/236. Lowest Common Ancestor of a Binary Tree.py new file mode 100644 index 00000000..1c5416f7 --- /dev/null +++ b/BST/Medium/236. Lowest Common Ancestor of a Binary Tree.py @@ -0,0 +1,12 @@ +#https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/ + +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, x): +# self.val = x +# self.left = None +# self.right = None + +class Solution: + def lowestCommonAncestor(self, root: 'TreeNode', p: 'TreeNode', q: 'TreeNode') -> 'TreeNode': + \ No newline at end of file diff --git a/BST/Medium/43_Delete_Node_In_A_BST.py b/BST/Medium/43_Delete_Node_In_A_BST.py index d6943680..e8a59490 100644 --- a/BST/Medium/43_Delete_Node_In_A_BST.py +++ b/BST/Medium/43_Delete_Node_In_A_BST.py @@ -1,6 +1,77 @@ # Problem: Delete_Node_In_A_BST # URL: https://leetcode.com/problems/delete-node-in-a-bst/ -def solution(): - # TODO: Implement the solution for Delete_Node_In_A_BST - pass +class Node: + def __init__(self,data) -> None: + self.val=data + self.left=None + self.right=None + + +class BST: + def insert(self,root,key): + if not root: + return Node(key) + if root.val>key: + root.left=self.insert(root.left,key) + elif root.valkey: + root.left=self.deleteNode(root.left,key) + elif root.val None: + """ + Do not return anything, modify root in-place instead. + """ + \ No newline at end of file diff --git a/BST/Traversal.py b/BST/Traversal.py index e69de29b..93040a84 100644 --- a/BST/Traversal.py +++ b/BST/Traversal.py @@ -0,0 +1,45 @@ +class Node: + def __init__(self,data): + self.left=None + self.right=None + self.data=data + +def insert(root,data): + if root is None: + return Node(data) + if root.data==data: + return root + if root.datadata: + root.left=insert(root.left,data) + return root + +def InOrder(root): + if root: + InOrder(root.left) + print(root.data, end=' ') + InOrder(root.right) +def preOrder(root): + if root: + print(root.data, end=' ') + preOrder(root.left) + preOrder(root.right) +def postOrder(root): + if root: + postOrder(root.left) + postOrder(root.right) + print(root.data, end=' ') + +if __name__ == '__main__': + root=Node(100) + insert(root,34) + insert(root,54) + insert(root,74) + insert(root,38) + insert(root,30) + InOrder(root) + print() + preOrder(root) + print() + postOrder(root) diff --git a/BST/Tree ds.ipynb b/BST/Tree ds.ipynb index efb5bd40..809694d8 100644 --- a/BST/Tree ds.ipynb +++ b/BST/Tree ds.ipynb @@ -4,13 +4,13 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "**Tree Data Structrue**
\n", - "A tree is a non-linear abstract data type with a hierarchy-based structure. It consists of nodes (where the data is stored) that are connected via links. The tree data structure stems from a single node called a root node and has subtrees connected to the root." + "**Tree val Structrue**
\n", + "A tree is a non-linear abstract val type with a hierarchy-based structure. It consists of nodes (where the val is stored) that are connected via links. The tree val structure stems from a single node called a root node and has subtrees connected to the root." ] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 38, "metadata": {}, "outputs": [], "source": [ @@ -27,7 +27,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 39, "metadata": {}, "outputs": [], "source": [ @@ -37,7 +37,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 40, "metadata": {}, "outputs": [], "source": [ @@ -48,7 +48,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 41, "metadata": {}, "outputs": [ { @@ -57,7 +57,7 @@ "{2: 2, 3: 2, 4: 5, 5: 1}" ] }, - "execution_count": 29, + "execution_count": 41, "metadata": {}, "output_type": "execute_result" } @@ -68,7 +68,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 42, "metadata": {}, "outputs": [], "source": [ @@ -77,7 +77,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 43, "metadata": {}, "outputs": [ { @@ -98,7 +98,7 @@ }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 44, "metadata": {}, "outputs": [], "source": [ @@ -107,7 +107,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 45, "metadata": {}, "outputs": [], "source": [ @@ -121,7 +121,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 46, "metadata": {}, "outputs": [ { @@ -130,7 +130,7 @@ "(4, 5)" ] }, - "execution_count": 57, + "execution_count": 46, "metadata": {}, "output_type": "execute_result" } @@ -141,7 +141,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 47, "metadata": {}, "outputs": [], "source": [ @@ -165,7 +165,7 @@ }, { "cell_type": "code", - "execution_count": 71, + "execution_count": 48, "metadata": {}, "outputs": [ { @@ -174,7 +174,7 @@ "(1, 1)" ] }, - "execution_count": 71, + "execution_count": 48, "metadata": {}, "output_type": "execute_result" } @@ -186,7 +186,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 49, "metadata": {}, "outputs": [], "source": [ @@ -202,7 +202,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 50, "metadata": {}, "outputs": [ { @@ -220,13 +220,13 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 51, "metadata": {}, "outputs": [], "source": [ "class TreeNode:\n", - " def __init__(self, data):\n", - " self.data = data\n", + " def __init__(self, val):\n", + " self.val = val\n", " self.left = None\n", " self.right = None\n", "\n", @@ -234,23 +234,23 @@ " def __init__(self):\n", " self.root = None\n", "\n", - " def insert(self, data):\n", + " def insert(self, val):\n", " if self.root is None:\n", - " self.root = TreeNode(data)\n", + " self.root = TreeNode(val)\n", " else:\n", - " self._insert(self.root, data)\n", + " self._insert(self.root, val)\n", "\n", - " def _insert(self, node, data):\n", - " if data < node.data:\n", + " def _insert(self, node, val):\n", + " if val < node.val:\n", " if node.left is None:\n", - " node.left = TreeNode(data)\n", + " node.left = TreeNode(val)\n", " else:\n", - " self._insert(node.left, data)\n", + " self._insert(node.left, val)\n", " else:\n", " if node.right is None:\n", - " node.right = TreeNode(data)\n", + " node.right = TreeNode(val)\n", " else:\n", - " self._insert(node.right, data)\n", + " self._insert(node.right, val)\n", "\n", " def inorder_traversal(self):\n", " return self._inorder_traversal(self.root, [])\n", @@ -258,7 +258,7 @@ " def _inorder_traversal(self, node, result):\n", " if node:\n", " self._inorder_traversal(node.left, result)\n", - " result.append(node.data)\n", + " result.append(node.val)\n", " self._inorder_traversal(node.right, result)\n", " return result\n", "\n", @@ -267,7 +267,7 @@ "\n", " def _preorder_traversal(self, node, result):\n", " if node:\n", - " result.append(node.data)\n", + " result.append(node.val)\n", " self._preorder_traversal(node.left, result)\n", " self._preorder_traversal(node.right, result)\n", " return result\n", @@ -279,13 +279,13 @@ " if node:\n", " self._postorder_traversal(node.left, result)\n", " self._postorder_traversal(node.right, result)\n", - " result.append(node.data)\n", + " result.append(node.val)\n", " return result\n" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 52, "metadata": {}, "outputs": [ { @@ -319,7 +319,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 53, "metadata": {}, "outputs": [], "source": [ @@ -330,23 +330,23 @@ " def __init__(self):\n", " self.root = None\n", "\n", - " def insert(self, data):\n", + " def insert(self, val):\n", " if self.root is None:\n", - " self.root = TreeNode(data)\n", + " self.root = TreeNode(val)\n", " else:\n", - " self._insert(self.root, data)\n", + " self._insert(self.root, val)\n", "\n", - " def _insert(self, node, data):\n", - " if data < node.data:\n", + " def _insert(self, node, val):\n", + " if val < node.val:\n", " if node.left is None:\n", - " node.left = TreeNode(data)\n", + " node.left = TreeNode(val)\n", " else:\n", - " self._insert(node.left, data)\n", + " self._insert(node.left, val)\n", " else:\n", " if node.right is None:\n", - " node.right = TreeNode(data)\n", + " node.right = TreeNode(val)\n", " else:\n", - " self._insert(node.right, data)\n", + " self._insert(node.right, val)\n", "\n", " def plot_tree(self):\n", " G = nx.DiGraph()\n", @@ -358,14 +358,14 @@ " def _add_edges(self, node, graph, parent=None):\n", " if node is not None:\n", " if parent:\n", - " graph.add_edge(parent.data, node.data)\n", + " graph.add_edge(parent.val, node.val)\n", " self._add_edges(node.left, graph, node)\n", " self._add_edges(node.right, graph, node)\n", "\n", " def _get_positions(self, node, x, y, dx):\n", " pos = {}\n", " if node:\n", - " pos[node.data] = (x, y)\n", + " pos[node.val] = (x, y)\n", " if node.left:\n", " pos.update(self._get_positions(node.left, x - dx, y - 1, dx / 2))\n", " if node.right:\n", @@ -383,18 +383,23 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 54, "metadata": {}, "outputs": [ { - "data": { - "image/png": "iVBORw0KGgoAAAANSUhEUgAAApQAAAHzCAYAA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CgYCygkGVBAOef6HD3MxvzAZ8wuTMb/H59tACQAAgMww6q0XAQAA4D4ESgAAAKSFQAkAAIC0ECgBAACQFgIlAAAA0kKgBAAAQFoIlAAAAEgLgRIAAABpIVACAAAgLQRKAAAApIVACQAAgLQQKAEAAJAWAiUAAADSQqAEAABAWgiUAAAASAuBEgAAAGkhUAIAACAtBEoAAACk5f8B8EH+bEyGfQsAAAAASUVORK5CYII=", - "text/plain": [ - "
" - ] - }, - "metadata": {}, - "output_type": "display_data" + "ename": "TypeError", + "evalue": "'int' object is not callable", + "output_type": "error", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[0;32mIn[54], line 14\u001b[0m\n\u001b[1;32m 11\u001b[0m bt\u001b[38;5;241m.\u001b[39minsert(\u001b[38;5;241m80\u001b[39m)\n\u001b[1;32m 13\u001b[0m \u001b[38;5;66;03m# Plot the tree\u001b[39;00m\n\u001b[0;32m---> 14\u001b[0m bt\u001b[38;5;241m.\u001b[39mplot_tree()\n", + "Cell \u001b[0;32mIn[53], line 29\u001b[0m, in \u001b[0;36mBinaryTree.plot_tree\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 27\u001b[0m G \u001b[38;5;241m=\u001b[39m nx\u001b[38;5;241m.\u001b[39mDiGraph()\n\u001b[1;32m 28\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_add_edges(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mroot, G)\n\u001b[0;32m---> 29\u001b[0m pos \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_positions(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mroot, \u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m0\u001b[39m, \u001b[38;5;241m2\u001b[39m \u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_depth(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mroot))\n\u001b[1;32m 30\u001b[0m nx\u001b[38;5;241m.\u001b[39mdraw(G, pos, with_labels\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, arrows\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m, node_size\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m2000\u001b[39m, node_color\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mlightblue\u001b[39m\u001b[38;5;124m'\u001b[39m, font_size\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m10\u001b[39m, font_weight\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mbold\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[1;32m 31\u001b[0m plt\u001b[38;5;241m.\u001b[39mshow()\n", + "Cell \u001b[0;32mIn[53], line 54\u001b[0m, in \u001b[0;36mBinaryTree._get_depth\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m 52\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m 53\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m---> 54\u001b[0m left_depth \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_depth(node\u001b[38;5;241m.\u001b[39mleft)\n\u001b[1;32m 55\u001b[0m right_depth \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_depth(node\u001b[38;5;241m.\u001b[39mright)\n\u001b[1;32m 56\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mmax\u001b[39m(left_depth, right_depth) \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m1\u001b[39m\n", + "Cell \u001b[0;32mIn[53], line 54\u001b[0m, in \u001b[0;36mBinaryTree._get_depth\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m 52\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;241m0\u001b[39m\n\u001b[1;32m 53\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m---> 54\u001b[0m left_depth \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_depth(node\u001b[38;5;241m.\u001b[39mleft)\n\u001b[1;32m 55\u001b[0m right_depth \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_depth(node\u001b[38;5;241m.\u001b[39mright)\n\u001b[1;32m 56\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mmax\u001b[39m(left_depth, right_depth) \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m1\u001b[39m\n", + "Cell \u001b[0;32mIn[53], line 56\u001b[0m, in \u001b[0;36mBinaryTree._get_depth\u001b[0;34m(self, node)\u001b[0m\n\u001b[1;32m 54\u001b[0m left_depth \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_depth(node\u001b[38;5;241m.\u001b[39mleft)\n\u001b[1;32m 55\u001b[0m right_depth \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_depth(node\u001b[38;5;241m.\u001b[39mright)\n\u001b[0;32m---> 56\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mmax\u001b[39m(left_depth, right_depth) \u001b[38;5;241m+\u001b[39m \u001b[38;5;241m1\u001b[39m\n", + "\u001b[0;31mTypeError\u001b[0m: 'int' object is not callable" + ] } ], "source": [ @@ -416,7 +421,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -494,7 +499,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -502,25 +507,25 @@ " def __init__(self, value) -> None:\n", " self.left = None\n", " self.right = None\n", - " self.data = value\n", + " self.val = value\n", "\n", " def preOrder(self, root):\n", " if root:\n", - " print(root.data, end=\" \") # Print the data of the root\n", + " print(root.val, end=\" \") # Print the val of the root\n", " self.preOrder(root.left) # Recur on the left child\n", " self.preOrder(root.right) # Recur on the right child\n", "\n", " def inOrder(self, root):\n", " if root:\n", " self.inOrder(root.left) # Recur on the left child\n", - " print(root.data, end=\" \") # Print the data of the root\n", + " print(root.val, end=\" \") # Print the val of the root\n", " self.inOrder(root.right) # Recur on the right child\n", "\n", " def postOrder(self, root):\n", " if root:\n", " self.postOrder(root.left) # Recur on the left child\n", " self.postOrder(root.right) # Recur on the right child\n", - " print(root.data, end=\" \") # Print the data of the root\n", + " print(root.val, end=\" \") # Print the val of the root\n", "\n", " def maxDepth(self, root) -> int:\n", " \n", @@ -528,23 +533,39 @@ " return 0\n", " left_depth = self.maxDepth(root.left)\n", " right_depth = self.maxDepth(root.right)\n", - " return right_depth\n", "\n", - " # return max(left_depth,right_depth) +1" + " return max(left_depth,right_depth) +1\n", + " \n", + " def LevelOrder(self,root):\n", + " if not root:\n", + " return\n", + " traversed=[]\n", + " queue=[root]\n", + " while len(queue)>0:\n", + " current=queue.pop(0)\n", + "\n", + " traversed.append(current.val)\n", + " if current.left:\n", + " queue.append(current.left)\n", + "\n", + " if current.right:\n", + " queue.append(current.right)\n", + "\n", + " return traversed\n" ] }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "0" + "[23, 21, 19, 17, 15, 17, 17, 17]" ] }, - "execution_count": 13, + "execution_count": 35, "metadata": {}, "output_type": "execute_result" } @@ -559,20 +580,21 @@ "root.right.right=Node(17)\n", "root.right.right.left=Node(17)\n", "# print(root.postOrder(root))\n", - "root.maxDepth(root)" + "# root.maxDepth(root)\n", + "root.LevelOrder(root)" ] }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ "class Node:\n", - " def __init__(self,data) -> None:\n", + " def __init__(self,val) -> None:\n", " self.left=None\n", " self.right=None\n", - " self.data=data\n", + " self.val=val\n", "\n", " def preOrder(self,root):\n", " stack=[]\n", @@ -580,7 +602,7 @@ " stack.append(root)\n", " while len(stack)>0:\n", " popped=stack.pop()\n", - " print(popped.data, end=\" \")\n", + " print(popped.val, end=\" \")\n", " if popped.right:\n", " stack.append(popped.right)\n", " if popped.left:\n", @@ -597,7 +619,7 @@ " currunt=currunt.left\n", " elif stack:\n", " currunt=stack.pop()\n", - " print(currunt.data, end=' ')\n", + " print(currunt.val, end=' ')\n", " currunt=currunt.right\n", "\n", " \n", @@ -610,7 +632,7 @@ " stack1.append(current)\n", " while stack1:\n", " current=stack1.pop()\n", - " stack2.append(current.data) \n", + " stack2.append(current.val) \n", "\n", " if current.left:\n", " stack1.append(current.left)\n", @@ -619,14 +641,30 @@ " stack1.append(current.right)\n", " \n", " while stack2:\n", - " data=stack2.pop()\n", - " print(data, end=' ')\n", - " " + " val=stack2.pop()\n", + " print(val, end=' ')\n", + " \n", + " def LevelOrder(self,root):\n", + " if not root:\n", + " return\n", + " traversed=[]\n", + " queue=deque([root])\n", + " while len(queue)>0:\n", + " current=queue.popleft()\n", + "\n", + " traversed.append(current.val)\n", + " if current.left:\n", + " queue.append(current.left)\n", + "\n", + " if current.right:\n", + " queue.append(current.right)\n", + "\n", + " return traversed" ] }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": {}, "outputs": [ { @@ -658,6 +696,286 @@ "root.postOrder(root)\n" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def buildTree(preorder, inorder):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "preorder=[\"C\",\"D\",\"B\",\"I\",\"A\",\"E\",\"F\",\"G\",\"H\"]\n", + "inOrder=[\"D\",\"I\",'A',\"B\",\"C\",\"E\",\"G\",\"H\",\"F\"]\n", + "buildTree(preorder,inOrder)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3, 9, 20, 15, 7]\n" + ] + } + ], + "source": [ + "# Definition for a binary tree node.\n", + "# class TreeNode:\n", + "# def __init__(self, val=0, left=None, right=None):\n", + "# self.val = val\n", + "# self.left = left\n", + "# self.right = right\n", + "class Solution:\n", + " def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:\n", + " if not inorder or not postorder:\n", + " return None\n", + " root=TreeNode(postorder[-1])\n", + " index=inorder.index(postorder[-1])\n", + " root.left=self.buildTree(inorder[:index],postorder[:index])\n", + " root.right=self.buildTree(inorder[index+1:],postorder[index:-1])\n", + " return root" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Definition for a binary tree node.\n", + "# class TreeNode:\n", + "# def __init__(self, val=0, left=None, right=None):\n", + "# self.val = val\n", + "# self.left = left\n", + "# self.right = right\n", + "class Solution:\n", + " def buildTree(self, inorder: List[int], preorder: List[int]) -> Optional[TreeNode]:\n", + " if not inorder or not preorder:\n", + " return None\n", + " root=TreeNode(preorder[0])\n", + " index=inorder.index(preorder[0])\n", + " root.left=self.buildTree(inorder[:index],preorder[1:index+1])\n", + " root.right=self.buildTree(inorder[index+1:],preorder[index+1:])\n", + " return root" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "from collections import deque\n", + "from typing import List\n", + "\n", + "class Solution:\n", + " def buildTree(self, inorder: List[int], preorder: List[int]):\n", + " if not inorder or not preorder:\n", + " return None\n", + " \n", + " root = TreeNode(preorder[0])\n", + " \n", + " index = inorder.index(preorder[0])\n", + " \n", + " root.left = self.buildTree(inorder[:index], preorder[1:index+1])\n", + " root.right = self.buildTree(inorder[index+1:], preorder[index+1:])\n", + " \n", + " return root" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## BST" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,val) -> None:\n", + " self.left=None\n", + " self.right=None\n", + " self.val=val\n", + "\n", + "\n", + "def inseart(root,val):\n", + " if not root:\n", + " return Node(val)\n", + " else:\n", + " if root.val==val:\n", + " return root\n", + " elif root.val root.val:\n", + " root.right = delete_ele(root.right, ele)\n", + " else:\n", + " # Node with only one child or no child\n", + " if not root.left:\n", + " return root.right\n", + " elif not root.right:\n", + " return root.left\n", + "\n", + " # Node with two children: Get the inorder successor\n", + " tempNode = minNode(root.right)\n", + " root.val = tempNode.val\n", + " root.right = delete_ele(root.right, tempNode.val)\n", + "\n", + " return root\n", + "\n", + "\n", + "\n", + "def find_InOrder_predeseccor(root):\n", + " current=root\n", + " while current.left:\n", + " current=current.left\n", + " return current.val" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "metadata": {}, + "outputs": [], + "source": [ + "if __name__==\"__main__\":\n", + " arr=[5,3,6,2,4,7]\n", + " root=Node(arr[0]) \n", + " for i in range(1,len(arr)):\n", + " root=inseart(root,arr[i])" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 3 6 2 4 7 " + ] + } + ], + "source": [ + "LevelOrder(root)" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "<__main__.Node at 0x7367a61ed4d0>" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "delete_ele(root,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 4 6 2 7 " + ] + } + ], + "source": [ + "LevelOrder(root)" + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/BST/big-o-cheatsheet.pdf b/BST/big-o-cheatsheet.pdf new file mode 100644 index 00000000..1bacafef Binary files /dev/null and b/BST/big-o-cheatsheet.pdf differ diff --git a/BST/file.sh b/BST/file.sh deleted file mode 100755 index e69de29b..00000000 diff --git a/Backtracking/Arithmetic_Expressions.py b/Backtracking/Arithmetic_Expressions.py new file mode 100644 index 00000000..fead10ea --- /dev/null +++ b/Backtracking/Arithmetic_Expressions.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.hackerrank.com/challenges/arithmetic-expressions/problem +# TODO: Implement the solution + +class Arithmetic_Expressions: + # TODO: Implement the solution for Arithmetic_Expressions.py + pass diff --git a/Backtracking/Combinational_Sum.py b/Backtracking/Combinational_Sum.py new file mode 100644 index 00000000..27f8fc16 --- /dev/null +++ b/Backtracking/Combinational_Sum.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/combinational-sum/ +# TODO: Implement the solution + +class Combinational_Sum: + # TODO: Implement the solution for Combinational_Sum.py + pass diff --git a/Backtracking/Cryptarithmetic_Puzzles.py b/Backtracking/Cryptarithmetic_Puzzles.py new file mode 100644 index 00000000..bb8bda96 --- /dev/null +++ b/Backtracking/Cryptarithmetic_Puzzles.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/backtracking-set-8-solving-cryptarithmetic-puzzles/ +# TODO: Implement the solution + +class Cryptarithmetic_Puzzles: + # TODO: Implement the solution for Cryptarithmetic_Puzzles.py + pass diff --git a/Backtracking/Hamiltonian_Cycle.py b/Backtracking/Hamiltonian_Cycle.py new file mode 100644 index 00000000..d13f8aab --- /dev/null +++ b/Backtracking/Hamiltonian_Cycle.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/backtracking-set-7-hamiltonian-cycle/ +# TODO: Implement the solution + +class Hamiltonian_Cycle: + # TODO: Implement the solution for Hamiltonian_Cycle.py + pass diff --git a/Backtracking/Knight_Tour.py b/Backtracking/Knight_Tour.py new file mode 100644 index 00000000..47e69885 --- /dev/null +++ b/Backtracking/Knight_Tour.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/backtracking-set-1-the-knights-tour-problem/ +# TODO: Implement the solution + +class Knight_Tour: + # TODO: Implement the solution for Knight_Tour.py + pass diff --git a/Backtracking/Kth_Permutation_Sequence.py b/Backtracking/Kth_Permutation_Sequence.py new file mode 100644 index 00000000..4de6adc6 --- /dev/null +++ b/Backtracking/Kth_Permutation_Sequence.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/find-the-k-th-permutation-sequence-of-first-n-natural-numbers/ +# TODO: Implement the solution + +class Kth_Permutation_Sequence: + # TODO: Implement the solution for Kth_Permutation_Sequence.py + pass diff --git a/Backtracking/Largest_Number_in_K_Swaps.py b/Backtracking/Largest_Number_in_K_Swaps.py new file mode 100644 index 00000000..763a6ab3 --- /dev/null +++ b/Backtracking/Largest_Number_in_K_Swaps.py @@ -0,0 +1,6 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/largest-number-in-k-swaps/0 +# TODO: Implement the solution + +class Largest_Number_in_K_Swaps: + # TODO: Implement the solution for Largest_Number_in_K_Swaps.py + pass diff --git a/Backtracking/Longest_Route_in_Matrix.py b/Backtracking/Longest_Route_in_Matrix.py new file mode 100644 index 00000000..764bf0bc --- /dev/null +++ b/Backtracking/Longest_Route_in_Matrix.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/longest-possible-route-in-a-matrix-with-hurdles/ +# TODO: Implement the solution + +class Longest_Route_in_Matrix: + # TODO: Implement the solution for Longest_Route_in_Matrix.py + pass diff --git a/Backtracking/M_Coloring_Problem.py b/Backtracking/M_Coloring_Problem.py new file mode 100644 index 00000000..f92def63 --- /dev/null +++ b/Backtracking/M_Coloring_Problem.py @@ -0,0 +1,6 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/m-coloring-problem-1587115620/1 +# TODO: Implement the solution + +class M_Coloring_Problem: + # TODO: Implement the solution for M_Coloring_Problem.py + pass diff --git a/Backtracking/Max_Number_With_K_Swaps.py b/Backtracking/Max_Number_With_K_Swaps.py new file mode 100644 index 00000000..1fc57f03 --- /dev/null +++ b/Backtracking/Max_Number_With_K_Swaps.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/find-maximum-number-possible-by-doing-at-most-k-swaps/ +# TODO: Implement the solution + +class Max_Number_With_K_Swaps: + # TODO: Implement the solution for Max_Number_With_K_Swaps.py + pass diff --git a/Backtracking/N_Queens.py b/Backtracking/N_Queens.py new file mode 100644 index 00000000..40c0a2e6 --- /dev/null +++ b/Backtracking/N_Queens.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/printing-solutions-n-queen-problem/ +# TODO: Implement the solution + +class N_Queens: + # TODO: Implement the solution for N_Queens.py + pass diff --git a/Backtracking/Palindromic_Partitions.py b/Backtracking/Palindromic_Partitions.py new file mode 100644 index 00000000..49906715 --- /dev/null +++ b/Backtracking/Palindromic_Partitions.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/print-palindromic-partitions-string/ +# TODO: Implement the solution + +class Palindromic_Partitions: + # TODO: Implement the solution for Palindromic_Partitions.py + pass diff --git a/Backtracking/Partition_Array_to_K_Subsets.py b/Backtracking/Partition_Array_to_K_Subsets.py new file mode 100644 index 00000000..0ec22b42 --- /dev/null +++ b/Backtracking/Partition_Array_to_K_Subsets.py @@ -0,0 +1,6 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/partition-array-to-k-subsets/1 +# TODO: Implement the solution + +class Partition_Array_to_K_Subsets: + # TODO: Implement the solution for Partition_Array_to_K_Subsets.py + pass diff --git a/Backtracking/Partition_Set_K_Subsets.py b/Backtracking/Partition_Set_K_Subsets.py new file mode 100644 index 00000000..1aefb3fc --- /dev/null +++ b/Backtracking/Partition_Set_K_Subsets.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/partition-set-k-subsets-equal-sum/ +# TODO: Implement the solution + +class Partition_Set_K_Subsets: + # TODO: Implement the solution for Partition_Set_K_Subsets.py + pass diff --git a/Backtracking/Path_With_Length.py b/Backtracking/Path_With_Length.py new file mode 100644 index 00000000..6e563365 --- /dev/null +++ b/Backtracking/Path_With_Length.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/find-if-there-is-a-path-of-more-than-k-length-from-a-source/ +# TODO: Implement the solution + +class Path_With_Length: + # TODO: Implement the solution for Path_With_Length.py + pass diff --git a/Backtracking/Path_from_Corner_to_Middle.py b/Backtracking/Path_from_Corner_to_Middle.py new file mode 100644 index 00000000..f842a414 --- /dev/null +++ b/Backtracking/Path_from_Corner_to_Middle.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/find-paths-from-corner-cell-to-middle-cell-in-maze/ +# TODO: Implement the solution + +class Path_from_Corner_to_Middle: + # TODO: Implement the solution for Path_from_Corner_to_Middle.py + pass diff --git a/Backtracking/Permutations_of_String.py b/Backtracking/Permutations_of_String.py new file mode 100644 index 00000000..6925d736 --- /dev/null +++ b/Backtracking/Permutations_of_String.py @@ -0,0 +1,6 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/permutations-of-a-given-string/0 +# TODO: Implement the solution + +class Permutations_of_String: + # TODO: Implement the solution for Permutations_of_String.py + pass diff --git a/Backtracking/Print_All_Paths.py b/Backtracking/Print_All_Paths.py new file mode 100644 index 00000000..15046c52 --- /dev/null +++ b/Backtracking/Print_All_Paths.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/print-all-possible-paths-from-top-left-to-bottom-right-of-a-mxn-matrix/ +# TODO: Implement the solution + +class Print_All_Paths: + # TODO: Implement the solution for Print_All_Paths.py + pass diff --git a/Backtracking/Rat_in_a_Maze.py b/Backtracking/Rat_in_a_Maze.py new file mode 100644 index 00000000..83559408 --- /dev/null +++ b/Backtracking/Rat_in_a_Maze.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/backttracking-set-2-rat-in-a-maze/ +# TODO: Implement the solution + +class Rat_in_a_Maze: + # TODO: Implement the solution for Rat_in_a_Maze.py + pass diff --git a/Backtracking/Rat_in_a_Maze_Problem.py b/Backtracking/Rat_in_a_Maze_Problem.py new file mode 100644 index 00000000..8180ec0e --- /dev/null +++ b/Backtracking/Rat_in_a_Maze_Problem.py @@ -0,0 +1,6 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/rat-in-a-maze-problem/1 +# TODO: Implement the solution + +class Rat_in_a_Maze_Problem: + # TODO: Implement the solution for Rat_in_a_Maze_Problem.py + pass diff --git a/Backtracking/Remove_Invalid_Parentheses.py b/Backtracking/Remove_Invalid_Parentheses.py new file mode 100644 index 00000000..7eaa7e0e --- /dev/null +++ b/Backtracking/Remove_Invalid_Parentheses.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/remove-invalid-parentheses/ +# TODO: Implement the solution + +class Remove_Invalid_Parentheses: + # TODO: Implement the solution for Remove_Invalid_Parentheses.py + pass diff --git a/Backtracking/Safe_Route_with_Landmines.py b/Backtracking/Safe_Route_with_Landmines.py new file mode 100644 index 00000000..787b930a --- /dev/null +++ b/Backtracking/Safe_Route_with_Landmines.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/find-shortest-safe-route-in-a-path-with-landmines/ +# TODO: Implement the solution + +class Safe_Route_with_Landmines: + # TODO: Implement the solution for Safe_Route_with_Landmines.py + pass diff --git a/Backtracking/Subset_Sum_Problem.py b/Backtracking/Subset_Sum_Problem.py new file mode 100644 index 00000000..39523641 --- /dev/null +++ b/Backtracking/Subset_Sum_Problem.py @@ -0,0 +1,6 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/subset-sum-problem2014/1 +# TODO: Implement the solution + +class Subset_Sum_Problem: + # TODO: Implement the solution for Subset_Sum_Problem.py + pass diff --git a/Backtracking/Sudoku.py b/Backtracking/Sudoku.py new file mode 100644 index 00000000..b8c0c3ad --- /dev/null +++ b/Backtracking/Sudoku.py @@ -0,0 +1,6 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/solve-the-sudoku-1587115621/1 +# TODO: Implement the solution + +class Sudoku: + # TODO: Implement the solution for Sudoku.py + pass diff --git a/Backtracking/Sudoku_Set_7.py b/Backtracking/Sudoku_Set_7.py new file mode 100644 index 00000000..bced72eb --- /dev/null +++ b/Backtracking/Sudoku_Set_7.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/backtracking-set-7-suduku/ +# TODO: Implement the solution + +class Sudoku_Set_7: + # TODO: Implement the solution for Sudoku_Set_7.py + pass diff --git a/Backtracking/Tug_of_War.py b/Backtracking/Tug_of_War.py new file mode 100644 index 00000000..e532a1b6 --- /dev/null +++ b/Backtracking/Tug_of_War.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/tug-of-war/ +# TODO: Implement the solution + +class Tug_of_War: + # TODO: Implement the solution for Tug_of_War.py + pass diff --git a/Backtracking/Word_Break_Part_2.py b/Backtracking/Word_Break_Part_2.py new file mode 100644 index 00000000..bb00bd13 --- /dev/null +++ b/Backtracking/Word_Break_Part_2.py @@ -0,0 +1,6 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/word-break-part-2/0 +# TODO: Implement the solution + +class Word_Break_Part_2: + # TODO: Implement the solution for Word_Break_Part_2.py + pass diff --git a/Backtracking/Word_Break_Problem.py b/Backtracking/Word_Break_Problem.py new file mode 100644 index 00000000..2194e964 --- /dev/null +++ b/Backtracking/Word_Break_Problem.py @@ -0,0 +1,6 @@ +# Problem URL: https://www.geeksforgeeks.org/word-break-problem-using-backtracking/ +# TODO: Implement the solution + +class Word_Break_Problem: + # TODO: Implement the solution for Word_Break_Problem.py + pass diff --git a/Heap data structure.ipynb b/Heaps & Hashing/Heap data structure.ipynb similarity index 100% rename from Heap data structure.ipynb rename to Heaps & Hashing/Heap data structure.ipynb diff --git a/heap creation.ipynb b/Heaps & Hashing/heap creation.ipynb similarity index 100% rename from heap creation.ipynb rename to Heaps & Hashing/heap creation.ipynb diff --git a/LINKED-LIST problems/Linked List Problems .ipynb b/LINKED-LIST problems/Linked List Problems .ipynb index 7a4f5e6d..f661f6b8 100644 --- a/LINKED-LIST problems/Linked List Problems .ipynb +++ b/LINKED-LIST problems/Linked List Problems .ipynb @@ -23,7 +23,24 @@ "\n", "5. **Deletion at the beginning:** Implement a function to delete a node from the beginning of a singly linked list.\n", "\n", - "6. **Deletion at the end:** Write a function to delete a node from the end of a singly linked list.\n" + "6. **Deletion at the end:** Write a function to delete a node from the end of a singly linked list.\n", + "7. **Deletion at a given position:** Implement a function to delete a node from a specified position in a singly linked list.\n", + "\n", + "8. **Search operation:** Write a function to search for a given value in a singly linked list.\n", + "\n", + "9. **Reverse a singly linked list:** Implement a function to reverse a singly linked list.\n", + "\n", + "10. **Detect loop in a linked list:** Write a function to detect if a loop exists in a singly linked list.\n", + "\n", + "11. **Find the middle of a linked list:** Implement a function to find the middle node of a singly linked list.\n", + "\n", + "12. **Merge two sorted linked lists:** Write a function to merge two sorted singly linked lists into a single sorted linked list.\n", + "\n", + "13. **Remove duplicates from a linked list:** Implement a function to remove duplicates from an unsorted singly linked list.\n", + "\n", + "14. **Check if a linked list is palindrome:** Write a function to check if a singly linked list is a palindrome.\n", + "\n", + "15. **Intersection point of two linked lists:** Implement a function to find the intersection point of two singly linked lists.\n" ] }, { @@ -366,7 +383,61 @@ " return \"SLL has no Node or only one Node!\"\n", " length = self.length()\n", " for i in range(length - 1):\n", - " n = self.head\n", + " n = self.head# https://leetcode.com/problems/double-a-number-represented-as-a-linked-list/description/\n", + "\n", + "# Definition for singly-linked list.\n", + "class ListNode:\n", + " def __init__(self, val=0, next=None):\n", + " self.val = val\n", + " self.next = next\n", + "class Solution:\n", + " def __init__(self) -> None:\n", + " self.head=None\n", + "\n", + " def insert(self,x):\n", + " new_node=ListNode(x)\n", + " new_node.next=self.head\n", + " self.head=new_node\n", + "\n", + " def print(self):\n", + " if self.head is not None:\n", + " n=self.head\n", + " while n:\n", + " print(n.val,end=' ')\n", + " n=n.next\n", + "\n", + " def clear(self):\n", + " self.head=None\n", + "\n", + " def doubleIt(self):\n", + " list=[]\n", + " n=self.head\n", + " while n:\n", + " a.append(n.val)\n", + " n=n.next\n", + " self.clear()\n", + " result = [int(''.join(map(str, list)))]\n", + " for i in result:\n", + " a = i * 2\n", + " result1 = [int(digit) for digit in str(a)]\n", + " for i in result1:\n", + " self.insert(i)\n", + " \n", + "\n", + " \n", + "if __name__ == '__main__':\n", + " s=Solution()\n", + " s.insert(3)\n", + " s.insert(2)\n", + " s.insert(1)\n", + " print()\n", + " s.print()\n", + " s.doubleIt()\n", + " s.print\n", + "\n", + "\n", + "\n", + " \n", " for j in range(length - i - 1):\n", " if n.data > n.next.data:\n", " n.data, n.next.data = n.next.data, n.data\n", @@ -850,423 +921,248 @@ }, { "cell_type": "markdown", - "id": "2f7f7b4e-fc67-425d-af01-b6ccfbc0183c", - "metadata": {}, - "source": [ - "- Print the Middle of a given linked list\n", - "- Reverse a Linked List\n", - "- Reverse a Doubly Linked List\n", - "- Rotate a linked list.\n", - "- Nth node from end of linked list ✔ \n", - "- Delete last occurrence of an item from linked list\n", - "- Delete middle of linked list\n", - "- Remove duplicate elements from sorted linked list\n", - "- Detect Loop in linked list\n", - "- Delete N nodes after M nodes of a linked list\n", - "- Merge a linked list into another linked list at alternate positions\n", - "- Circular Linked List Traversal\n", - "- Deletion from a Circular Linked List\n", - "- Delete without head pointer\n", - "- Implement Queue using Linked List\n", - "- Implement a stack using singly linked list\n", - "- Remove every k-th node of the linked list\n", - "- Pairwise swap of a Linked list\n", - "- Occurrence of an integer in a Linked List\n", - "- Given a Linked list of 0s, 1s and 2s, sort it\n", - "- Deletion in Linked List\n" + "id": "7dae4818-def0-4e6f-9197-08cfcecdba6e", + "metadata": {}, + "source": [ + "\n", + "# 3. 🤝 Find the Merge Point of Two Linked Lists\n" ] }, { "cell_type": "code", - "execution_count": 81, - "id": "590cf9d2-60bc-436a-8017-dd8c8f7d0928", + "execution_count": null, + "id": "47e15829-fbd2-44d7-8f3c-576382b76e8d", "metadata": {}, "outputs": [], - "source": [ - "class Node:\n", - " def __init__(self,data):\n", - " self.data=data\n", - " self.next=None\n", - "\n", - "class SynglyLL:\n", - " def __init__(self):\n", - " self.head=None\n", - " \n", - " def print(self):\n", - " if self.head is None: return \"SLL Empty!\"\n", - " n=self.head\n", - " while n:\n", - " print(n.data, end=' ')\n", - " n=n.next\n", - " \n", - " def Reverse(self):\n", - " if self.head is None:\n", - " return \"LL Empty!\"\n", - " current = self.head\n", - " prev = None\n", - " while currentxd is not None:\n", - " next_node = current.next\n", - " current.next = prev\n", - " prev = current\n", - " current = next_node\n", - " self.head = prev\n", - " return prev\n", - " \n", - "\n", - " def add_begin(self,x):\n", - " new_node=Node(x)\n", - " new_node.next=self.head\n", - " self.head=new_node\n", - " \n", - " def insert_end(self,x):\n", - " new_node=Node(x)\n", - " if self.head is None:\n", - " self.add_begin(x)\n", - " return\n", - " n=self.head\n", - " while n.next:\n", - " n=n.next\n", - " n.next=new_node\n", - " \n", - " def length(self):\n", - " count=0\n", - " n=self.head\n", - " while n:\n", - " count+=1\n", - " n=n.next\n", - " return count\n", - " \n", - " \n", - " def Middle__LL(self):\n", - " if self.length()==0: return \"Singly LL is Empty!\"\n", - " mid=self.length()//2\n", - " n=self.head\n", - " index = 0\n", - " while index < mid:\n", - " n = n.next\n", - " index += 1\n", - " return n.data\n", - " \n", - " \n", - "SLL=SynglyLL() " - ] + "source": [] }, { - "cell_type": "code", - "execution_count": 82, - "id": "fcbddc90-6da3-447b-bade-db242d558ccc", + "cell_type": "markdown", + "id": "8450f872-977e-4484-99bd-7e4d9a93af66", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'SLL Empty!'" - ] - }, - "execution_count": 82, - "metadata": {}, - "output_type": "execute_result" - } - ], "source": [ - "SLL.print()" + "\n", + "# 4. 🚫 Remove N-th Node From End of List\n" ] }, { "cell_type": "code", - "execution_count": 89, - "id": "bf137b02-4211-4f10-98e9-4fcf560a0d3a", + "execution_count": null, + "id": "9e1ad243-1aac-4184-83c2-74d383fbd6b6", "metadata": {}, "outputs": [], - "source": [ - "SLL.add_begin(56)" - ] + "source": [] }, { - "cell_type": "code", - "execution_count": 84, - "id": "d3aa0174-cf02-4546-9c8b-b3196980aeae", + "cell_type": "markdown", + "id": "845ea2ad-0810-48ad-b1d4-740fddb77674", "metadata": {}, - "outputs": [], "source": [ - "SLL.insert_end(3)" + "\n", + "# 5. 🔗 Merge Two Sorted Linked Lists\n" ] }, { "cell_type": "code", - "execution_count": 91, - "id": "db7d377f-8d65-4116-adb2-3c62d52d7cc1", + "execution_count": null, + "id": "bcb40684-4c82-47bd-aba4-e0e00d8cfd79", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "3" - ] - }, - "execution_count": 91, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "SLL.length()" - ] + "outputs": [], + "source": [] }, { - "cell_type": "code", - "execution_count": 92, - "id": "c662979e-47e2-4406-bfa9-1c0cce03daf9", + "cell_type": "markdown", + "id": "36abf3d4-7063-4d75-ac31-1e59c1e474a4", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "56" - ] - }, - "execution_count": 92, - "metadata": {}, - "output_type": "execute_result" - } - ], "source": [ - "SLL.Middle__LL()" + "\n", + "# 6. 🖼️ Check if a Linked List is a Palindrome\n" ] }, { "cell_type": "code", "execution_count": null, - "id": "954f93ff-c699-4a19-a987-daa9cc288107", + "id": "5a56d1ae-e814-4387-871e-182a62595959", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "2e5fedee-bfa9-4f1a-afba-28e4fbd75f2f", - "metadata": {}, - "source": [ - "**Medium Level Problems:**\n", - "1. Convert singly linked list into circular linked list\n", - "2. Reverse a linked list in groups of given size\n", - "3. Merge two sorted linked lists\n", - "4. Remove loop in Linked List\n", - "5. Function to check if a singly linked list is palindrome\n", - "6. Remove all occurrences of one Linked list in another Linked list\n", - "7. Intersection point in Y shaped Linked lists\n", - "8. Intersection of two Sorted Linked Lists\n", - "9. Split a Circular Linked List into two halves\n", - "10. Find pairs with given sum in doubly linked list\n", - "11. Remove duplicates from an unsorted doubly linked list\n", - "12. Intersection point of two Linked Lists.\n", - "13. Add two numbers represented by Linked lists\n", - "14. Multiply two numbers represented by Linked Lists\n", - "15. Swap Kth node from beginning with Kth node from end in a Linked List\n", - "16. Sort a k sorted doubly linked list\n", - "17. Rotate Doubly linked list by N nodes\n", - "18. Convert a Binary Tree into Doubly Linked List in spiral fashion\n", - "19. Convert a given Binary Tree to Doubly Linked List\n", - "20. Construct a linked list from 2D matrix\n", - "21. Reverse a doubly linked list in groups of given size\n" + "id": "24b06d25-8872-4b70-b101-8250330a039e", + "metadata": {}, + "source": [ + "\n", + "# 7. 🚨 Remove Duplicates from a Sorted List\n" ] }, { "cell_type": "code", "execution_count": null, - "id": "50ce07dd-c3fc-4f92-8edd-995ea4b1fd5f", + "id": "3758f388-6237-49e9-91c8-8d7726fb65a4", "metadata": {}, "outputs": [], "source": [] }, { - "cell_type": "code", - "execution_count": 92, - "id": "24364723-c0a0-4c5a-8e03-9c66ac2b809c", + "cell_type": "markdown", + "id": "13aaf7dd-3b30-490f-af06-193356d38784", "metadata": {}, - "outputs": [], "source": [ - "from typing import List" + "\n", + "# 8. 🎯 Find the Middle of a Linked List\n" ] }, { "cell_type": "code", "execution_count": null, - "id": "be4ddea7-4dd8-4fcc-835c-649aa136faf4", + "id": "ee0e005b-0906-4276-a48d-710d513b0c9a", "metadata": {}, "outputs": [], "source": [] }, + { + "cell_type": "markdown", + "id": "18e53811-0fd4-40da-9cc2-e41c4df98a8b", + "metadata": {}, + "source": [ + "\n", + "# 9. 🔄 Rotate a Linked List\n" + ] + }, { "cell_type": "code", "execution_count": null, - "id": "8d7ccd68-7d50-4ae4-b5e8-002d414cefa8", + "id": "9c5142d9-cf81-4207-8a3d-7df5427e9cdd", "metadata": {}, "outputs": [], "source": [] }, + { + "cell_type": "markdown", + "id": "9121b674-5fa5-459d-81f1-770c45bdf816", + "metadata": {}, + "source": [ + "\n", + "# 10. 📑 Implement a Doubly Linked List\n" + ] + }, { "cell_type": "code", "execution_count": null, - "id": "d1fab8ba-df48-401b-ac31-5b183d2f7990", + "id": "4ee2a457-a57e-4270-9069-c75c47086672", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", - "id": "c7911678-ca0f-4ca9-988e-0af9053a4e89", + "id": "364f2c2a-86af-4b19-9821-400a64d85893", "metadata": {}, "source": [ "\n", - "**Hard Level Problems:**\n", - "1. Reverse a sublist of linked list\n", - "2. Rearrange a given linked list in-place.\n", - "3. Reverse Alternate K Nodes in a Singly Linked List:\n", - "4. Reverse a Linked List in groups of given size\n", - "5. Merge k Sorted Linked Lists\n", - "6. Flattening a Linked List\n", - "7. Partition a linked list around a given value\n", - "8. Clone a linked list with random pointers\n", - "\n", - "These problems range from easy to hard, covering various operations and manipulations on linked lists." + "# 11. 📊 Implement a Circular Linked List\n" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "fb778604-b4e2-4b56-bad2-6a6f5d01415d", + "metadata": {}, + "outputs": [], + "source": [] + }, { "cell_type": "markdown", - "id": "7b695e24", + "id": "67e03aa6-b593-4e64-b112-84340a85c639", "metadata": {}, "source": [ - "### Reverse a Doubly Linked List\n" + "\n", + "# 12. 🛠️ Add Two Numbers Represented by Linked Lists\n" ] }, { "cell_type": "code", - "execution_count": 93, - "id": "ddb5c371", + "execution_count": null, + "id": "66392f56-a2c2-47f0-8b4e-eaebf6c0b5eb", "metadata": {}, "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "6094fcf9-feaa-4dde-9b16-e364daa711fe", + "metadata": {}, "source": [ - "class Node:\n", - " def __init__(self,data):\n", - " self.data=data\n", - " self.prev=None\n", - " self.next=None\n", - "class DLL:\n", - " def __init__(self):\n", - " self.head=None\n", - " \n", - " def display(self):\n", - " if self.head is not None:\n", - " if self.head.next is None:\n", - " return self.head.data\n", - " n=self.head\n", - " while n:\n", - " print(n.data,end=' ')\n", - " n=n.next\n", - " \n", - " def push_end(self,data):\n", - " new_node=Node(data)\n", - " if self.head is None:\n", - " self.head=new_node\n", - " return\n", - " n=self.head\n", - " while n.next:\n", - " n=n.next\n", - " n.next=new_node\n", - " new_node.prev=n\n", - " \n", - " def Reverse(self):\n", - " if self.head is not None:\n", - " n=self.head\n", - " temp=None\n", - " while n:\n", - " temp=n.prev\n", - " n.prev=n.next\n", - " n.next=temp\n", - " n=n.prev\n", - " if temp:\n", - " self.head=temp.prev\n", - " \n", - " " + "\n", + "# 13. 🧹 Remove Linked List Elements\n" ] }, { "cell_type": "code", - "execution_count": 58, - "id": "459984ac", + "execution_count": null, + "id": "854d93b7-361c-4ede-9447-2ae4a04b923a", "metadata": {}, "outputs": [], - "source": [ - "Dll=DLL()" - ] + "source": [] }, { - "cell_type": "code", - "execution_count": 61, - "id": "4f756367", + "cell_type": "markdown", + "id": "0c0a1ae1-49fd-4171-98c7-edf2da446f85", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 61, - "metadata": {}, - "output_type": "execute_result" - } - ], "source": [ - "Dll.display()" + "\n", + "# 14. 🧩 Partition List around a value" ] }, { "cell_type": "code", - "execution_count": 60, - "id": "0d4929f8", + "execution_count": null, + "id": "794c5a6b-0fc1-47ca-93da-42772b64eef9", "metadata": {}, "outputs": [], + "source": [] + }, + { + "cell_type": "markdown", + "id": "224b2b38-80ab-4b90-8686-63a53c2c641d", + "metadata": {}, "source": [ - "Dll.push_end(1)" + "\n", + "\n", + "# 15. 🔄 Reverse Nodes in k-Group" ] }, { "cell_type": "code", - "execution_count": 62, - "id": "98a0c74a", + "execution_count": null, + "id": "0fcbb10a-5cc1-4e7a-b523-27e0917962a8", "metadata": {}, "outputs": [], - "source": [ - "Dll.Reverse()" - ] + "source": [] }, { - "cell_type": "code", - "execution_count": 63, - "id": "01761b72", + "cell_type": "markdown", + "id": "fcc07c89-ecf8-44e0-8796-dcf719e587ad", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 63, - "metadata": {}, - "output_type": "execute_result" - } - ], "source": [ - "Dll.display()" + "**complexities of various operations on a linked list:**\n", + "| Operation | Time Complexity |\n", + "|-------------------------------|-----------------|\n", + "| Insertion at Beginning | \\(O(1)\\) |\n", + "| Insertion at End | \\(O(n)\\) |\n", + "| Insertion at Specific Position| \\(O(n)\\) |\n", + "| Deletion at Beginning | \\(O(1)\\) |\n", + "| Deletion at End | \\(O(n)\\) |\n", + "| Deletion at Specific Position | \\(O(n)\\) |\n", + "| Search | \\(O(n)\\) |\n", + "| Access/Traversal | \\(O(n)\\) |\n" ] }, { "cell_type": "code", - "execution_count": 420, - "id": "fda5093f", + "execution_count": 77, + "id": "42556b24-ce59-4ac5-8b73-816426442f98", "metadata": {}, "outputs": [], "source": [ @@ -1274,330 +1170,2751 @@ " def __init__(self,data):\n", " self.data=data\n", " self.next=None\n", - "class Sll:\n", + " \n", + "class SynglyLL:\n", " def __init__(self):\n", " self.head=None\n", - " self.tail=None\n", + "\n", + " def len(self):\n", + " if self.head is None: return 0\n", + " count=0\n", + " n=self.head\n", + " while n:\n", + " n=n.next\n", + " count+=1\n", + " return count\n", + " \n", + " def insert_begin(self,data):\n", + " new_node=Node(data)\n", + " new_node.next=self.head\n", + " self.head=new_node\n", + " return\n", " \n", - " def append(self,data):\n", + " def print(self):\n", + " if self.head is None: return \"SLL is Empty!\"\n", + " n=self.head\n", + " while n:\n", + " print(n.data, end= ' ')\n", + " n=n.next\n", + " \n", + " def insert_end(self,data):\n", " new_node=Node(data)\n", - " if self.head==None:\n", - " self.head=new_node\n", - " self.tail=new_node\n", - " return data\n", - " self.tail.next=new_node\n", - " self.tail=new_node\n", - " return new_node.data\n", - " \n", - " def display(self):\n", - " if self.head!=None:\n", - " n=self.head\n", - " while n:\n", - " print(n.data,end=' ')\n", - " n=n.next\n", - " \n", - " def middleNode(self):\n", - " length=0\n", + " if self.head is None:\n", + " self.insert_begin(data)\n", + " return\n", " n=self.head\n", - " if n is not None:\n", - " while n:\n", - " length+=1\n", - " n=n.next\n", - " mid=length//2\n", + " while n.next:\n", + " n=n.next\n", + " n.next=new_node\n", + " \n", + " def insert_by_value(self,data,x):\n", + " new_node=Node(data)\n", + " if self.head is None: return \"Empty! LL!\"\n", " n=self.head\n", - " for i in range(mid):\n", + " while n:\n", + " if n.data==x:\n", + " new_node.next=n.next\n", + " n.next=new_node\n", + " return\n", " n=n.next\n", - " return n.data\n", + " print(f\"{x} Node is not present in LL!\")\n", + " \n", + " def insert_mid(self,data):\n", + " new_node=Node(data)\n", + " if self.head is None:\n", + " self.insert_begin(data)\n", + " return\n", + " mid=self.len()//2\n", + " n=self.len()\n", + " if n%2==1:\n", + " mid +=1\n", + " n=self.head\n", + " while mid>1:\n", + " n=n.next\n", + " mid -=1\n", + " new_node.next=n.next\n", + " n.next=new_node\n", + " \n", + " def insert_after(self,data,x):\n", + " new_node=Node(data)\n", + " n=self.head\n", + " while n:\n", + " if n.data==x:\n", + " new_node.next=n.next\n", + " n.next=new_node\n", + " return\n", + " n=n.next\n", + " print(f\"{x} node is not present in SLL or SLL is Empty!\") \n", + "\n", + " def clear(self):\n", + " self.head=None\n", + " \n", + " def remove_begin(self):\n", + " if self.head is None:\n", + " raise IndexError (\"Empty! LL\")\n", + " return\n", + " self.head=self.head.next\n", + " \n", + " def remove_end(self):\n", + " if self.head is None:\n", + " raise IndexError (\"Empty! LL\")\n", + " return\n", + " if self.head.next is None:\n", + " self.head=None\n", + " return\n", + " n=self.head\n", + " while n.next.next:\n", + " n=n.next\n", + " n.next=None\n", + " \n", + " def delete_Node(self,x):\n", + " if self.head is None:\n", + " raise IndexError (\"Empty! LL\")\n", + " return\n", + " if self.head.data==x:\n", + " self.head=self.head.next\n", + " return\n", + " n=self.head\n", + " while n:\n", + " if n.data==x:\n", + " n.next=n.next.next\n", + " n=n.next\n", + " print(f\"{x} node is not present in SLL!\")\n", + "\n", + " def search(self,x):\n", + " if self.head is None:\n", + " raise IndexError (\"Empty! LL\")\n", + " return\n", + " n=self.head\n", + " count=0\n", + " while n:\n", + " if n.data==x:\n", + " print(f\"{x} is present at index {count}\")\n", + " return\n", + " count+=1\n", + " n=n.next\n", + " print(f\"{x} node is not present in SLL!\")\n", + "\n", + " def search_by_index(self,x):\n", + " if self.head is None:\n", + " raise IndexError (\"Empty! LL\")\n", + " return\n", + " if x>self.len():\n", + " raise IndexError (\"Given Index is out Of range!\")\n", + " return\n", + " if x==0:\n", + " return self.head.data\n", + " n=self.head\n", + " while x!=0:\n", + " x -=1\n", + " n=n.next \n", + " print(n.data) \n", + " \n", + " def delete_by_index(self,x):\n", + " if self.head is None:\n", + " raise IndexError (\"Empty! LL\")\n", + " return\n", + " if x>=self.len():\n", + " raise IndexError (\"Given Index is out Of range!\")\n", + " return\n", + " if x==0:\n", + " self.head=self.head.next\n", + " return\n", + " if x==self.len()-1:\n", + " self.remove_end()\n", + " return \n", + " n = self.head\n", + " while x != 1:\n", + " n = n.next\n", + " x -= 1\n", + " n.next = n.next.next\n", + " \n", + " def Reverse(self):\n", + " if self.head is None:\n", + " raise IndexError (\"Empty! LL\")\n", + " return\n", + " if self.head.next is None:\n", + " self.head.data\n", + " return \n", + " cur=self.head\n", + " prev=None\n", + " while cur:\n", + " temp=cur.next\n", + " cur.next=prev\n", + " prev=cur\n", + " cur=temp\n", + " self.head=prev\n", + "\n", + " def mid_of_sll(self):\n", + " if self.head is None:\n", + " raise IndexError(\"Empty! LL\")\n", " \n", - " #Nth node from end of linked list\n", - " def Nth_node(self,n):\n", - " length=0\n", - " x=self.head\n", - " if x is not None:\n", - " while x:\n", - " length+=1\n", - " x=x.next \n", - " if n <= 0 or n > length:\n", - " return None\n", - " x=self.head\n", - " for i in range(n-1):\n", - " x=x.next\n", - " return x.data\n", + " mid = self.len() // 2\n", + " if self.len() % 2 == 1:\n", + " mid += 1\n", " \n", - " def getNthFromLast(self,n):\n", - " first = self.head\n", - " second = self.head\n", - "\n", - " for _ in range(n):\n", - " if not first:\n", - " return None\n", - " first = first.next\n", + " n = self.head\n", + " while mid != 0:\n", + " n = n.next\n", + " mid -= 1\n", + " \n", + " return n.data\n", "\n", - " while first:\n", - " first = first.next\n", - " second=second.next\n", - " return second.data\n", + " \n", + " def Reverse_k_element(self, k):\n", + " if self.head is None:\n", + " raise IndexError(\"Empty! LL\")\n", + " return\n", + " if k>self.len() or k<=0:\n", + " raise IndexError (\"Given Index is out Of range!\")\n", + " return\n", + " n=self.head\n", + " prev=None\n", " \n", - " def skipMdeleteN(self,M):\n", + " def replace_max(self,data):\n", + " if self.head is None:\n", + " raise IndexError(\"Empty! LL\")\n", + " return\n", " n=self.head\n", - " if self.head.next is not None:\n", - " if self.head.data==M:\n", - " self.head.next=self.head.next.next\n", - " return\n", + " max=n\n", + " while n:\n", + " if n.data>max.data:\n", + " max= n\n", + " n=n.next\n", + " max.data=data\n", + " \n", + " def get_odd_index_value(self):\n", + " if self.head is None:\n", + " return 0\n", + " \n", + " n = self.head\n", + " cur = 0\n", + " result = 0\n", + " \n", + " while n:\n", + " if cur % 2 != 0:\n", + " result += n.data\n", + " cur += 1\n", + " n = n.next\n", + " \n", + " return result\n", + " \n", + " \n", + " \n", + "Sll=SynglyLL() " + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "id": "7a643579-fe84-42c5-a189-8cd36ecaae8c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "22" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Sll.get_odd_index_value()" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "id": "1ac15268-d411-4fae-a6fc-3888a6a673aa", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "20 1 3 20 1 1 " + ] + } + ], + "source": [ + "Sll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "id": "6096ff7d-48a1-4d09-85e9-cf657136a693", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.replace_max(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 648, + "id": "5c6317f1-a3c5-4565-9146-2221186e5954", + "metadata": {}, + "outputs": [], + "source": [ + "# Sll.Reverse_k_element(0)" + ] + }, + { + "cell_type": "code", + "execution_count": 706, + "id": "32ebe16b-8b4d-4f59-b696-64be4a3d3414", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "30" + ] + }, + "execution_count": 706, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Sll.mid_of_sll()" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "7b784e82-3ad2-43dd-9340-3b3d4a354715", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.insert_begin(1)\n", + "Sll.insert_begin(1)\n", + "Sll.insert_begin(20)\n", + "Sll.insert_begin(3)\n", + "Sll.insert_begin(1)\n", + "Sll.insert_begin(20)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 608, + "id": "cc1b7d23-bddb-4af8-8b8f-41f370a0a19d", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.Reverse()" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "94aea40c-decd-4ac6-97d9-8d0e784b8d5f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3 1 1 " + ] + } + ], + "source": [ + "Sll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "edc0e050-3159-4625-b444-60d19fa69913", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "20 10 30 " + ] + } + ], + "source": [ + "Sll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 572, + "id": "af374a0c-384f-4635-8192-43ecf44b0adc", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.delete_by_index(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 594, + "id": "a1a4bb68-8ab5-4d57-8121-74883ad05df4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23 2 4 3 " + ] + } + ], + "source": [ + "Sll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "77c91097-7a5f-4d6b-bc4d-0e3eae828ab7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "20 10 30 " + ] + } + ], + "source": [ + "Sll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 700, + "id": "93f906b8-a1cc-4cf8-b5ba-0f882cb689ee", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.insert_mid(23)\n", + "Sll.insert_mid(3)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6ea3d0c4-f377-4b09-b57b-2ed56954197d", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5ea956cb-e2fb-434c-a4ce-14b1b9e865b4", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 375, + "id": "d686fed1-4912-4a9a-a305-9a0ebab476d2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3\n" + ] + } + ], + "source": [ + "Sll.search_by_index(3)" + ] + }, + { + "cell_type": "code", + "execution_count": 576, + "id": "77682589-fbe9-46ad-b1d2-2cb0d3ad0bdf", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23 " + ] + } + ], + "source": [ + "Sll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 469, + "id": "46e082db-99dc-43bb-9a32-7878bcf4bb67", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 469, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Sll.len()" + ] + }, + { + "cell_type": "code", + "execution_count": 368, + "id": "1d02d05e-0ba4-4b4c-8f2d-82358710d62f", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.insert_mid(23)\n", + "Sll.insert_mid(3)\n", + "Sll.insert_mid(2)\n", + "Sll.insert_mid(4)" + ] + }, + { + "cell_type": "code", + "execution_count": 225, + "id": "04ab3ea3-614a-4a80-bec9-0980f49631ae", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 is present at index 1\n" + ] + } + ], + "source": [ + "Sll.search(2)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3ba7e7a2-0523-4b6b-a368-3b72556e3521", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 168, + "id": "59f22499-e967-44c9-aabb-acb2bc0b49f4", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.delete_Node(23)" + ] + }, + { + "cell_type": "code", + "execution_count": 268, + "id": "8bf98093-7348-48c3-ae22-2f26418461de", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23 3 23 2 " + ] + } + ], + "source": [ + "Sll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 163, + "id": "752d1e91-fd3e-49a1-a05b-10fc0aa05f0c", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.remove_end()" + ] + }, + { + "cell_type": "code", + "execution_count": 223, + "id": "60a05ba7-b240-4b6b-8482-985c00d99db1", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 194, + "id": "d135b2b2-3432-4c50-a0f1-af380aaf5372", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23 node is not present in SLL or SLL is Empty!\n" + ] + } + ], + "source": [ + "Sll.insert_after(0,23)" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "id": "cc603b46-4f8e-47aa-ae49-17e779bd9cdb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "execution_count": 110, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Sll.len()" + ] + }, + { + "cell_type": "code", + "execution_count": 129, + "id": "3ab47c1b-3535-4800-91c9-c17a7acd02b7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'SLL is Empty!'" + ] + }, + "execution_count": 129, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Sll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 663, + "id": "248f88f4-c111-472d-a582-52a6018ed4ec", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.clear()" + ] + }, + { + "cell_type": "code", + "execution_count": 551, + "id": "3b9a59ca-b19d-4feb-acd0-812c423b8580", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.insert_begin(30)" + ] + }, + { + "cell_type": "code", + "execution_count": 184, + "id": "1afa28d4-46bd-46ff-b493-12f7552bc94e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14 14 " + ] + }, + { + "data": { + "text/plain": [ + "2" + ] + }, + "execution_count": 184, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Sll.print()\n", + "Sll.len()" + ] + }, + { + "cell_type": "code", + "execution_count": 556, + "id": "b1c580f6-4d86-49fb-b30b-f5d4a89b6abc", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.insert_end(11)" + ] + }, + { + "cell_type": "code", + "execution_count": 170, + "id": "0e2aa244-7b46-4300-9b5e-abe2d6f94a60", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "21 Node is not present in LL!\n" + ] + } + ], + "source": [ + "Sll.insert_by_value(25,21)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7f673360-4dd5-4184-a62a-18eba6a4d8ad", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3eb3fd97-5ceb-48bb-a12c-263e4d16735c", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 405, + "id": "724d0353-a06f-41ec-ac65-0a8c48e4ae67", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "execution_count": 405, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "b=53\n", + "result = b // 2 # This will result in 1\n", + "if b % 2 == 1: # Check if there's a remainder\n", + " result += 1 # Add 1 to the result\n", + "result" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f1f940d7-f7ad-495d-9ab8-4d7ab2d5ac81", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 426, + "id": "9440974a-c712-41d9-bf62-f37c169dce9e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "26" + ] + }, + "execution_count": 426, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "mid=53//2\n", + "if mid % 2 == 1: # Check if there's a remainder\n", + " mid += 1 # Add 1 to the result\n", + "mid" + ] + }, + { + "cell_type": "code", + "execution_count": 421, + "id": "ca2f4d66-7820-49da-b926-ad5367290144", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 421, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "3//2" + ] + }, + { + "cell_type": "code", + "execution_count": 451, + "id": "5ece34a5-449f-4df6-8a0f-72f2cb3f1448", + "metadata": {}, + "outputs": [], + "source": [ + "n=53//2\n", + "m = 53 # This results in m = 26\n", + "if m % 2 == 1: # Checks if m is odd\n", + " n += 1 # Increments m by 1 if it's odd" + ] + }, + { + "cell_type": "code", + "execution_count": 452, + "id": "ba114919-8760-48d7-9f4f-98a4b1d71238", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "27" + ] + }, + "execution_count": 452, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "n" + ] + }, + { + "cell_type": "code", + "execution_count": 448, + "id": "c0190adf-4a01-4d79-a9f6-81cfc3db8a9e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "26" + ] + }, + "execution_count": 448, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "m" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "5120a887-08f7-4606-90f0-a5ae487a6552", + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data=data\n", + " self.next=None\n", + " \n", + "class SinglyLL:\n", + " def __init__(self):\n", + " self.head=None\n", + "\n", + " def insert_begin(self,data):\n", + " new_node=Node(data)\n", + " if self.head is None:\n", + " self.head=new_node\n", + " return\n", + " new_node.next=self.head\n", + " self.head=new_node\n", + "\n", + " def insert_end(self,data):\n", + " new_node=Node(data)\n", + " if self.head is None:\n", + " self.head=new_node\n", + " return\n", + " n=self.head\n", + " while n.next is not None:\n", + " n=n.next\n", + " n.next=new_node\n", + " \n", + " def _print(self):\n", + " if self.head is None:\n", + " return \"linked list is Empty!\"\n", + " else:\n", + " n=self.head\n", + " while n is not None:\n", + " print(n.data, end=' ')\n", + " n=n.next\n", + " print()\n", + " \n", + " def len(self):\n", + " if self.head is None: return 0\n", + " n=self.head\n", + " count=0\n", + " while n:\n", + " count+=1\n", + " n=n.next\n", + " return count\n", + " \n", + " \n", + " def Reverse(self):\n", + " prev=None\n", + " curr=self.head\n", + " for i in range(self.len()):\n", + " temp=curr.next\n", + " curr.next=prev\n", + " prev=curr\n", + " curr=temp\n", + " self.head=prev\n", + " \n", + " \n", + "sll=SinglyLL()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0c846d35-fdd3-4b8e-9944-268adfeca393", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "73555790-8d4c-4099-a2b6-b7b09d1f9da5", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2f9886f8-6213-4967-9284-dd17c2e1f77c", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d3b0111f-7911-4d12-8820-fdc684ddc0ec", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "8fbb9b97-777d-4d4f-b9e1-78268d058e31", + "metadata": {}, + "outputs": [], + "source": [ + "sll.insert_begin(1)\n", + "sll.insert_begin(2)\n", + "sll.insert_begin(3)\n", + "sll.insert_begin(4)\n", + "sll.insert_begin(5)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "c5f1e179-53fb-419c-abb2-ecfcb4137942", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 4 3 2 1 1 2 3 4 5 \n" + ] + } + ], + "source": [ + "sll._print()" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "2a8c3f9a-6159-42a2-8b64-94e71a1d0a8f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sll.len()" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "869761fd-7980-4852-9bff-12d0fe5d7bf7", + "metadata": {}, + "outputs": [], + "source": [ + "sll.Reverse()" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "id": "585d75ee-2fb6-4368-8d7e-152e615faeca", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 4 3 2 1 1 2 3 4 5 \n" + ] + } + ], + "source": [ + "sll._print()" + ] + }, + { + "cell_type": "markdown", + "id": "e6356f84-7b6e-400d-bca0-7ee0726ddb50", + "metadata": {}, + "source": [ + "## **Problem Statement:**\n", + "\n", + "Given a singly linked list containing characters, write a Python function to return a new string created by appending all the characters from the linked list following these rules:\n", + "\n", + "1. Replace '*' or '/' with a single space.\n", + "2. If there are two consecutive occurrences of '*' or '/', replace those occurrences with a single space and convert the next character to uppercase.\n", + " \n", + "Assumptions:\n", + "\n", + "- There will not be more than two consecutive occurrences of '*' or '/'.\n", + "- The linked list will always end with an alphabet.\n", + "\n", + "**Example:**\n", + "\n", + "Input Linked List: A, n, *, /, a, p, ., p, l, e, *, a, /, d, a, y, *, *, k, e, e, p, s, ., /, ., *, a, /, /, d, o, c, t, o, r, *, A, w, a, y\n", + "\n", + "Output: \"AN APPLE A DAY KEEPS AWAY\"" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "id": "5e2c752f-dc87-44af-a408-71ba4eea7fae", + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data=data\n", + " self.next=None\n", + " \n", + "class SinglyLL:\n", + " def __init__(self):\n", + " self.head=None\n", + "\n", + " def insert_begin(self,data):\n", + " new_node=Node(data)\n", + " if self.head is None:\n", + " self.head=new_node\n", + " return\n", + " new_node.next=self.head\n", + " self.head=new_node\n", + "\n", + " def insert_end(self,data):\n", + " new_node=Node(data)\n", + " if self.head is None:\n", + " self.head=new_node\n", + " return\n", + " n=self.head\n", + " while n.next is not None:\n", + " n=n.next\n", + " n.next=new_node\n", + " \n", + " def _print(self):\n", + " if self.head is None:\n", + " return \"linked list is Empty!\"\n", + " else:\n", + " n=self.head\n", + " while n is not None:\n", + " print(n.data, end='')\n", + " n=n.next\n", + " print()\n", + " \n", + " def len(self):\n", + " if self.head is None: return 0\n", + " n=self.head\n", + " count=0\n", + " while n:\n", + " count+=1\n", + " n=n.next\n", + " return count\n", + " \n", + " \n", + " def Reverse(self):\n", + " prev=None\n", + " curr=self.head\n", + " for i in range(self.len()):\n", + " temp=curr.next\n", + " curr.next=prev\n", + " prev=curr\n", + " curr=temp\n", + " self.head=prev\n", + " \n", + " def change_sentence(self):\n", + " if self.head is None: return \"Empty LL!\"\n", + " n= self.head\n", + " while n:\n", + " if n.data== \"*\" or n.data==\"/\":\n", + " n.data=\" \"\n", + " if n.next.data== \"*\" or n.next.data== \"/\":\n", + " n.next.next.data=n.next.next.data.upper()\n", + " n.next=n.next.next\n", + " n=n.next\n", + " \n", + "Sl=SinglyLL()" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "id": "ad5f6f83-4460-4630-a68f-18bc2e910cef", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'linked list is Empty!'" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Sl._print()" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "id": "3b08210c-d854-4551-879a-4dd6180d14fa", + "metadata": {}, + "outputs": [], + "source": [ + "Sl.insert_end('A')\n", + "Sl.insert_end('n')\n", + "Sl.insert_end('*')\n", + "Sl.insert_end('/')\n", + "Sl.insert_end('a')\n", + "Sl.insert_end('p')\n", + "Sl.insert_end('p')\n", + "Sl.insert_end('l')\n", + "Sl.insert_end('e')\n", + "Sl.insert_end('*')\n", + "Sl.insert_end('a')\n", + "Sl.insert_end('/')\n", + "Sl.insert_end('d')\n", + "Sl.insert_end('a')\n", + "Sl.insert_end('y')\n", + "Sl.insert_end('*')\n", + "Sl.insert_end('*')\n", + "Sl.insert_end('k')\n", + "Sl.insert_end('e')\n", + "Sl.insert_end('e')\n", + "Sl.insert_end('p')\n", + "Sl.insert_end('s')\n", + "Sl.insert_end('/')\n", + "Sl.insert_end('*')\n", + "Sl.insert_end('a')\n", + "Sl.insert_end('/')\n", + "Sl.insert_end('/')\n", + "Sl.insert_end('d')\n", + "Sl.insert_end('o')\n", + "Sl.insert_end('c')\n", + "Sl.insert_end('t')\n", + "Sl.insert_end('o')\n", + "Sl.insert_end('r')\n", + "Sl.insert_end('*')\n", + "Sl.insert_end('A')\n", + "Sl.insert_end('w')\n", + "Sl.insert_end('a')\n", + "Sl.insert_end('y')" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "id": "1f4e88a0-bd2c-41b0-947f-fc46158021f6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "An*/apple*a/day**keeps/*a//doctor*Away\n" + ] + } + ], + "source": [ + "Sl._print()" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "id": "f1271771-181d-49da-959b-2855a43674be", + "metadata": {}, + "outputs": [], + "source": [ + "Sl.change_sentence()" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "id": "d3d804f6-7c27-4740-b8a6-922009ac7c32", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "An Apple a day Keeps A Doctor Away\n" + ] + } + ], + "source": [ + "Sl._print()" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "id": "6101b892-75d0-491c-a1fa-6d72f2ec3a0d", + "metadata": {}, + "outputs": [], + "source": [ + "# https://leetcode.com/problems/double-a-number-represented-as-a-linked-list/description/\n", + "\n", + "# Definition for singly-linked list.\n", + "class ListNode:\n", + " def __init__(self, val=0, next=None):\n", + " self.val = val\n", + " self.next = next\n", + "class Solution:\n", + " def __init__(self) -> None:\n", + " self.head=None\n", + "\n", + " def insert(self,x):\n", + " new_node=ListNode(x)\n", + " new_node.next=self.head\n", + " self.head=new_node\n", + "\n", + " def print(self):\n", + " if self.head is not None:\n", + " n=self.head\n", + " while n:\n", + " print(n.val,end=' ')\n", + " n=n.next\n", + "\n", + " def clear(self):\n", + " self.head=None\n", + "\n", + " def doubleIt(self):\n", + " list=[]\n", + " n=self.head\n", + " while n:\n", + " list.append(n.val)\n", + " n=n.next\n", + " self.clear()\n", + " result = [int(''.join(map(str, list)))]\n", + " for i in result:\n", + " a = i * 2\n", + " result1 = [int(digit) for digit in str(a)]\n", + " reult1=result1[::-1]\n", + " for i in result1:\n", + " self.insert(i)\n", + " return self.print()\n", + " \n", + "\n", + "\n", + "\n", + "s=Solution()" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "id": "43e17bc9-099f-4f5a-bcfc-3b1f63428564", + "metadata": {}, + "outputs": [], + "source": [ + "s.insert(13)" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "id": "0aeca022-0635-4d07-9bc6-deccbdacf325", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "13 6 " + ] + } + ], + "source": [ + "s.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "id": "984dd3a0-46f9-409a-bb8b-fca6fca304b6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 7 2 " + ] + } + ], + "source": [ + "s.doubleIt()" + ] + }, + { + "cell_type": "code", + "execution_count": 183, + "id": "90965987-2e8a-4085-a63e-a6c4f766d3ae", + "metadata": {}, + "outputs": [], + "source": [ + "# https://www.geeksforgeeks.org/reverse-a-linked-list/\n", + "\n", + "class Node:\n", + " def __init__(self, data):\n", + " self.data = data\n", + " self.next = None\n", + "\n", + "class LinkedList:\n", + " def __init__(self):\n", + " self.head = None\n", + " self.tail = None\n", + "\n", + " def insert_begin(self, data):\n", + " new_node = Node(data)\n", + " if self.head is None:\n", + " self.head = new_node\n", + " self.tail = new_node\n", + " else:\n", + " new_node.next = self.head\n", + " self.head = new_node\n", + "\n", + " def insert_end(self, data):\n", + " new_node = Node(data)\n", + " if self.head is None:\n", + " self.head = new_node\n", + " self.tail = new_node\n", + " else:\n", + " self.tail.next = new_node\n", + " self.tail = new_node\n", + "\n", + " def print(self):\n", + " current = self.head\n", + " while current:\n", + " print(current.data, end=' ')\n", + " current = current.next\n", + " print()\n", + " \n", + " def Reverse(self):\n", + " if self.head is None:\n", + " return \"LL Empty!\"\n", + " current = self.head\n", + " prev = None\n", + " while current is not None:\n", + " next_node = current.next\n", + " current.next = prev\n", + " prev = current\n", + " current = next_node\n", + "\n", + " self.head = prev\n", + " return prev\n", + "\n", + " def rotate(self, k):\n", + " pass\n", + " \n", + " def clear(self):\n", + " self.head=None\n", + "\n", + "S=LinkedList()" + ] + }, + { + "cell_type": "code", + "execution_count": 171, + "id": "2197c32d-c770-48b8-829b-b7fa7f688d3d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "S.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 186, + "id": "21542d1a-b4aa-4a8c-ad4b-3e401c3c235d", + "metadata": {}, + "outputs": [], + "source": [ + "S.insert_begin(14)" + ] + }, + { + "cell_type": "code", + "execution_count": 187, + "id": "73f997d4-c344-4b6e-8747-5c4c6669a2e8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14 4 \n" + ] + } + ], + "source": [ + "S.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 188, + "id": "fb3a9753-4a13-414f-bf4e-bcbd4a32e911", + "metadata": {}, + "outputs": [], + "source": [ + "S.insert_end(17)" + ] + }, + { + "cell_type": "code", + "execution_count": 189, + "id": "1eb243ae-78c2-412c-81f8-324817a70e53", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "14 4 17 \n" + ] + } + ], + "source": [ + "S.print()" + ] + }, + { + "cell_type": "markdown", + "id": "c912cf69-a3cc-4ac7-94c3-a01c4a72b475", + "metadata": {}, + "source": [ + "1. **Create a singly linked list:** Implement a class or structure for a singly linked list, including methods to add nodes, remove nodes, and print the list.\n", + "\n", + "2. **Insertion at the beginning:** Write a function to insert a node at the beginning of a singly linked list.\n", + "\n", + "3. **Insertion at the end:** Implement a function to insert a node at the end of a singly linked list.\n", + "\n", + "4. **Insertion at a given position:** Write a function to insert a node at a specified position in a singly linked list.\n", + "\n", + "5. **Deletion at the beginning:** Implement a function to delete a node from the beginning of a singly linked list.\n", + "\n", + "6. **Deletion at the end:** Write a function to delete a node from the end of a singly linked list.\n", + "7. **Deletion at a given position:** Implement a function to delete a node from a specified position in a singly linked list.\n", + "\n", + "8. **Search operation:** Write a function to search for a given value in a singly linked list.\n", + "\n", + "9. **Reverse a singly linked list:** Implement a function to reverse a singly linked list.\n", + "\n", + "10. **Detect loop in a linked list:** Write a function to detect if a loop exists in a singly linked list.\n", + "\n", + "11. **Find the middle of a linked list:** Implement a function to find the middle node of a singly linked list.\n", + "\n", + "12. **Merge two sorted linked lists:** Write a function to merge two sorted singly linked lists into a single sorted linked list.\n", + "\n", + "13. **Remove duplicates from a linked list:** Implement a function to remove duplicates from an unsorted singly linked list.\n", + "\n", + "14. **Check if a linked list is palindrome:** Write a function to check if a singly linked list is a palindrome.\n", + "\n", + "15. **Intersection point of two linked lists:** Implement a function to find the intersection point of two singly linked lists.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 601, + "id": "1625cb39-fd93-494b-8be3-dddca3629da8", + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data=data\n", + " self.next=None\n", + "class Sll:\n", + " def __init__(self):\n", + " self.head=None\n", + " self.tail=None\n", + " \n", + " def append(self,data):\n", + " new_node=Node(data)\n", + " if self.head==None or self.head.data >=data:\n", + " new_node.next=self.head\n", + " self.head=new_node\n", + " self.tail=new_node\n", + " return data\n", + " n=self.head\n", + " while n.next is not None and n.next.data < data:\n", + " n = n.next\n", + " new_node.next=n.next\n", + " n.next=new_node\n", + " if new_node.next is None:\n", + " self.tail = new_node\n", + " \n", + " def display(self):\n", + " if self.head !=None:\n", + " n=self.head\n", + " while n:\n", + " print(n.data,end=' ')\n", + " n=n.next\n", + " if n == self.head:\n", + " break\n", + " \n", + " def reverse(self):\n", + " curr=self.head\n", + " prev=None\n", + " self.tail = self.head\n", + " while curr:\n", + " temp=curr.next\n", + " curr.next=prev\n", + " prev=curr\n", + " curr=temp\n", + " self.head=prev\n", + " #148. Sort List leetcode\n", + " \n", + " def _split_list(self, head):\n", + " if head is None or head.next is None:\n", + " return head, None\n", + " slow = head\n", + " fast = head.next\n", + "\n", + " while fast and fast.next:\n", + " slow = slow.next\n", + " fast = fast.next.next\n", + "\n", + " middle = slow.next\n", + " slow.next = None\n", + "\n", + " return head, middle\n", + "\n", + " def _split_list(self, head):\n", + " if head is None or head.next is None:\n", + " return head, None\n", + " slow = head\n", + " fast = head.next\n", + "\n", + " while fast and fast.next:\n", + " slow = slow.next\n", + " fast = fast.next.next\n", + "\n", + " middle = slow.next\n", + " slow.next = None\n", + "\n", + " return head, middle\n", + "\n", + " def _merge_sorted_lists(self, left, right):\n", + " dummy = Node(0)\n", + " tail = dummy\n", + " while left and right:\n", + " if left.data <= right.data:\n", + " tail.next = left\n", + " left = left.next\n", + " else:\n", + " tail.next = right\n", + " right = right.next\n", + " tail = tail.next\n", + " tail.next = left if left else right\n", + " return dummy.next\n", + "\n", + " def _merge_sort(self, head):\n", + " if head is None or head.next is None:\n", + " return head\n", + " left, right = self._split_list(head)\n", + " left = self._merge_sort(left)\n", + " right = self._merge_sort(right)\n", + " return self._merge_sorted_lists(left, right)\n", + " def sort(self):\n", + " self.head = self._merge_sort(self.head)\n", + " \n", + " def detecte_loop(self):\n", + " fast=self.head\n", + " slow=self.head\n", + " while fast and fast.next:\n", + " slow=slow.next\n", + " fast=fast.next.next\n", + " if slow==fast:\n", + " return True\n", + " return False\n", + " \n", + " def appendleft(self, data):\n", + " new_node = Node(data)\n", + " if self.head is None: # If the list is empty\n", + " self.head = new_node\n", + " new_node.next = self.head\n", + " self.tail = new_node\n", + " return data\n", + " new_node.next=self.head\n", + " self.tail.next=new_node\n", + " self.head=new_node\n", + " return data\n", + " \n", + " def Search (self,x):\n", + " n=self.head\n", + " if n !=None:\n", + " while n:\n", + " if n.data==x:\n", + " return True\n", + " n=n.next\n", + " return False\n", + " \n", + " def middle(self):\n", + " length=0\n", + " n=self.head\n", + " while n:\n", + " length+=1\n", + " n=n.next\n", + " mid=length//2\n", + " n=self.head\n", + " while mid!=0:\n", + " n=n.next\n", + " mid-=1\n", + " return n.data\n", + " \n", + " #Deletion at a given position\n", + " def delete_i(self, x):\n", + " if x == 0:\n", + " if self.head is not None:\n", + " self.head = self.head.next\n", + " return\n", + " n = self.head\n", + " while n is not None and x > 1:\n", + " n = n.next\n", + " x -= 1\n", + " if n is None or n.next is None:\n", + " return\n", + " n.next = n.next.next\n", + " \n", + " \n", + "S1=Sll()" + ] + }, + { + "cell_type": "code", + "execution_count": 602, + "id": "a838f823-249b-42b6-ba19-2d6f9612ef8a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 602, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "S1.append(9)" + ] + }, + { + "cell_type": "code", + "execution_count": 603, + "id": "8e05494f-2829-4c9d-8876-417fd61fcb30", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "9 " + ] + } + ], + "source": [ + "S1.display()" + ] + }, + { + "cell_type": "code", + "execution_count": 604, + "id": "1a4112dd-aeee-4ef9-8842-a040af12c2da", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 604, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "S1.middle()" + ] + }, + { + "cell_type": "code", + "execution_count": 605, + "id": "58110af7-7567-448a-90d9-72416b190d64", + "metadata": {}, + "outputs": [], + "source": [ + "S1.delete_i(0)" + ] + }, + { + "cell_type": "code", + "execution_count": 606, + "id": "5ac74765-b144-467c-8acd-f670a75ca961", + "metadata": {}, + "outputs": [], + "source": [ + "S1.display()" + ] + }, + { + "cell_type": "code", + "execution_count": 1028, + "id": "16f497f2-1edd-4e74-a9f7-edf0ef6f9367", + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self, data):\n", + " self.data = data\n", + " self.next = None\n", + "\n", + "class LinkedList:\n", + " def __init__(self):\n", + " self.head = None\n", + " self.tail = None\n", + "\n", + " def append(self, data):\n", + " new_node = Node(data)\n", + " if self.head is None:\n", + " self.head = new_node\n", + " self.tail = new_node\n", + " else:\n", + " self.tail.next = new_node\n", + " self.tail = new_node\n", + "\n", + " def print(self):\n", + " current = self.head\n", + " while current:\n", + " print(current.data, end=' ')\n", + " current = current.next\n", + " \n", + " def Remove_ele(self,char):\n", + " try:\n", + " x=self.head.data\n", + " if self.head.data==char:\n", + " self.head=self.head.next\n", + " return x\n", + " n=self.head\n", + " while n:\n", + " if n.next.data==char:\n", + " x=n.next.data\n", + " n.next=n.next.next\n", + " return x\n", + " n=n.next\n", + " return -1\n", + " except:\n", + " return -1\n", + " \n", + " def Remove_similer_element(self,char):\n", + " while self.head and self.head.data == char:\n", + " self.head = self.head.next\n", + "\n", + " current = self.head\n", + " while current and current.next:\n", + " if current.next.data == char:\n", + " current.next = current.next.next\n", + " else:\n", + " current = current.next\n", + "\n", + " return\n", + "\n", + " \n", + " \n", + "sll=LinkedList()" + ] + }, + { + "cell_type": "code", + "execution_count": 1029, + "id": "858c58cf-e528-4dad-97d5-83dcceb635de", + "metadata": {}, + "outputs": [], + "source": [ + "a=\"cccccccccccccdr\"\n", + "for i in a:\n", + " sll.append(i)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 1030, + "id": "35c119ea-41be-47b9-bcab-e97ea60105c4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "c c c c c c c c c c c c c d r " + ] + } + ], + "source": [ + "sll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 1031, + "id": "1858d0c4-9738-4c81-8b63-10272ac955ec", + "metadata": {}, + "outputs": [], + "source": [ + "sll.Remove_similer_element(\"d\")" + ] + }, + { + "cell_type": "code", + "execution_count": 1032, + "id": "86f7ba1e-2e62-4907-b357-eb43795f91ed", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "c c c c c c c c c c c c c r " + ] + } + ], + "source": [ + "sll.print()" + ] + }, + { + "cell_type": "markdown", + "id": "b2af8314-d1a6-4fad-af03-271fc377dfa9", + "metadata": {}, + "source": [ + "| | Question |\n", + "|-------|-------------------------------------------------------------------------------------------------|\n", + "| 1 | Write a Program to reverse the Linked List. (Both Iterative and recursive) |\n", + "| 2 | Reverse a Linked List in group of Given Size. [Very Imp] |\n", + "| 3 | Write a program to Detect loop in a linked list. |\n", + "| 4 | Write a program to Delete loop in a linked list. |\n", + "| 5 | Find the starting point of the loop. |\n", + "| 6 | Remove Duplicates in a sorted Linked List. |\n", + "| 7 | Remove Duplicates in a Un-sorted Linked List. |\n", + "| 8 | Write a Program to Move the last element to Front in a Linked List. |\n", + "| 9 | Add “1” to a number represented as a Linked List. |\n", + "| 10 | Add two numbers represented by linked lists. |\n", + "| 11 | Intersection of two Sorted Linked List. |\n", + "| 12 | Intersection Point of two Linked Lists. |\n", + "| 13 | Merge Sort For Linked lists.[Very Important] |\n", + "| 14 | Quicksort for Linked Lists.[Very Important] |\n", + "| 15 | Find the middle Element of a linked list. |\n", + "| 16 | Check if a linked list is a circular linked list. |\n", + "| 17 | Split a Circular linked list into two halves. |\n", + "| 18 | Write a Program to check whether the Singly Linked list is a palindrome or not. |\n", + "| 19 | Deletion from a Circular Linked List. |\n", + "| 20 | Reverse a Doubly Linked list. |\n", + "| 21 | Find pairs with a given sum in a DLL. |\n", + "| 22 | Count triplets in a sorted DLL whose sum is equal to given value “X”. |\n", + "| 23 | Sort a “k”sorted Doubly Linked list.[Very IMP] |\n", + "| 24 | Rotate DoublyLinked list by N nodes. |\n", + "| 25 | Rotate a Doubly Linked list in group of Given Size.[Very IMP] |\n", + "| 26 | Can we reverse a linked list in less than O(n) ? |\n", + "| 27 | Why Quicksort is preferred for. Arrays and Merge Sort for LinkedLists ? |\n", + "| 28 | Flatten a Linked List |\n", + "| 29 | Sort a LL of 0's, 1's and 2's |\n", + "| 30 | Clone a linked list with next and random pointer |\n", + "| 31 | Merge K sorted Linked list |\n", + "| 32 | Multiply 2 no. represented by LL |\n", + "| 33 | Delete nodes which have a greater value on right side |\n", + "| 34 | Segregate even and odd nodes in a Linked List |\n", + "| 35 | Program for n’th node from the end of a Linked List |\n", + "| 36 | Find the first non-repeating character from a stream of characters |" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "00b3ebff-a26e-48bd-85ff-5fed6d1e42d9", + "metadata": {}, + "outputs": [], + "source": [ + "import Slinked_list as s\n", + "Sll=s.LinkedList()" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "f23081da-6eca-4bd0-a1b3-3590fcad045b", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.append(8)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6558339c-577d-4e14-8b9d-551bb6250e51", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "8 " + ] + } + ], + "source": [ + "Sll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "c42b3377-c23e-487f-97be-0b6779564d3b", + "metadata": {}, + "outputs": [], + "source": [ + "def clear(self):\n", + " self.head = None\n", + "s.LinkedList.clear = clear" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "3209bd23-1025-4412-a4c9-190bae71348d", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.clear()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "59febb67-6054-461a-942f-4142b2521558", + "metadata": {}, + "outputs": [], + "source": [ + "# reverse the Linked List. (Both Iterative and recursive)\n", + "def reverse(self):\n", + " n=self.head\n", + " prev=None\n", + " while n:\n", + " temp=n.next\n", + " n.next=prev\n", + " prev=n\n", + " n=temp\n", + " self.head=prev\n", + "s.LinkedList.reverse = reverse" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "77333d3f-d9d6-4d0d-802f-e40ff305cd74", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.reverse()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "f91957fe-169a-46ba-bceb-1c19851eb16f", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "539949ea-a062-4141-9860-222f2b65a223", + "metadata": {}, + "outputs": [], + "source": [ + "Sl=s.LinkedList" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "3e063050-9488-4dd3-8676-77285d2c18d1", + "metadata": {}, + "outputs": [], + "source": [ + "# recursive\n", + "def _reverse_recursive(self, current, prev):\n", + " if not current:\n", + " return prev\n", + " next_node = current.next\n", + " current.next = prev\n", + " return self._reverse_recursive(next_node, current)\n", + "\n", + "def reverse2(self):\n", + " self.head = self._reverse_recursive(self.head, None)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "7385e51f-72c3-41a7-a641-6add0090198f", + "metadata": {}, + "outputs": [], + "source": [ + "Sl.reverse2=reverse2" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "9e4b89fd-b4b5-40ee-a31e-2dee6f9b0d3b", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "132f601c-21ef-4ca5-a2b5-6f242aece27d", + "metadata": {}, + "outputs": [], + "source": [ + "#Write a program to Detect loop in a linked list.\n", + "def detect_loop(self):\n", + " n=self.head\n", + " try:\n", + " if self.head.next==self.head:\n", + " return True\n", + " while n:\n", + " if n.next==self.head:\n", + " return True\n", + " n=n.next\n", + " except:\n", + " return False\n", + "Sl.detect_loop=detect_loop" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "4e4a9714-eabe-43ef-8365-34bb0590a9d6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Sll.detect_loop()" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "75b1704a-d696-4ef2-a9ec-1557a6e42272", + "metadata": {}, + "outputs": [], + "source": [ + "def append_circuler(self,data):\n", + " new_node=s.Node(data)\n", + " if self.head ==None:\n", + " self.head=new_node\n", + " self.tail=new_node\n", + " self.tail.next=self.head\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "8a7db87a-748e-43aa-94ee-ea72205cf7eb", + "metadata": {}, + "outputs": [], + "source": [ + "Sl.append_circuler=append_circuler" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "4b899075-e8f0-4733-adfe-721fa2556388", + "metadata": {}, + "outputs": [], + "source": [ + "Sll.append_circuler(12)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "82e6a572-0e22-4c2a-8a5a-e73457ead80d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Sll.detect_loop()" + ] + }, + { + "cell_type": "code", + "execution_count": 81, + "id": "0cbbb0ba-ffbf-429a-9ac7-53956c5a5f24", + "metadata": {}, + "outputs": [], + "source": [ + "k=s.Node" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "68dbeef3-2788-4f38-b2be-6e540de90066", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 98, + "id": "f96656c9-736b-40f6-b829-ee19c531d2c1", + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,data):\n", + " self.data=data\n", + " self.next=None\n", + "class Sll:\n", + " def __init__(self):\n", + " self.head=None\n", + " self.tail=None\n", + " \n", + " def __len__(self):\n", + " count=0\n", " n=self.head\n", - " while n.next:\n", - " if M==n.data:\n", - " n.next=n.next.next\n", + " while n:\n", + " count+=1\n", " n=n.next\n", - " return -1\n", + " return count\n", " \n", - " def deleteNode(self, key):\n", - " n=self.head\n", + " def append(self,data):\n", + " new_node=Node(data)\n", " if self.head==None:\n", - " return \"not found!\"\n", - " if self.head.data==key:\n", - " self.head=self.head.next\n", - " return\n", - " if self.head.next is not None:\n", - " while n:\n", - " if n.data==key:\n", - " n.next=n.next.next\n", + " self.head=new_node\n", + " self.tail=new_node\n", + " return data\n", + " self.tail.next=new_node\n", + " self.tail=new_node\n", + " return data\n", + " \n", + " def __str__(self):\n", + " nodes=[]\n", + " if self.head !=None:\n", + " n=self.head\n", + " while n!=None:\n", + " nodes.append(n.data)\n", + " if n.next==self.head:\n", + " break\n", " n=n.next\n", - " return \"not found!\"\n", + " return str(nodes)\n", " \n", - " def deleteK(self,k):\n", + " def reverse_k(self, k):\n", + " if self.head is not None:\n", + " curr = self.head\n", + " temp_head=self.head\n", + " prev = None\n", + " while curr and k:\n", + " temp = curr.next # Store the next node\n", + " curr.next = prev # Reverse the link\n", + " prev = curr # Move prev and curr one step forward\n", + " curr = temp\n", + " k -= 1\n", + " temp_head.next = temp\n", + " self.head = prev # Update the head of the list to the new front\n", + " \n", + " #\tWrite a program to Delete loop in a linked list.\n", + " def delete_loop(self):\n", " pass\n", - " \n", - " def reverse(self):\n", - " if self.head is None:\n", - " return \"LL Empty!\"\n", - " current = self.head\n", - " prev = None\n", - " while current is not None:\n", - " next_node = current.next\n", - " current.next = prev\n", - " prev = current\n", - " current = next_node\n", - " self.head = prev\n", - " \n", + "\n", "SLL=Sll()" ] }, { "cell_type": "code", - "execution_count": 424, - "id": "f0a5cce4-2ed3-41e8-922b-e3a1f052cb21", + "execution_count": 99, + "id": "74dced1a-071a-4edf-9946-8008a5157cab", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "7" + "0" ] }, - "execution_count": 424, + "execution_count": 99, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "SLL.append(7)" + "len(SLL)" ] }, { "cell_type": "code", - "execution_count": 426, - "id": "c44b4de4-4133-41e6-a668-09fdf152c049", + "execution_count": 100, + "id": "2ed513ba-35d5-4944-8989-b83c7f6b8b4c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 100, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "SLL.reverse()" + "SLL.append(5)" ] }, { "cell_type": "code", - "execution_count": 427, - "id": "412c8e9d-ebf9-4035-a9e9-20591ef26c04", + "execution_count": 101, + "id": "daf8c628-d498-44f8-bcec-9f38587fba62", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "7 6 " + "[5]\n" ] } ], "source": [ - "SLL.display()" + "print(SLL)" ] }, { "cell_type": "code", - "execution_count": 405, - "id": "f5c0a12f-98ab-4b83-99fc-866a107e3098", + "execution_count": 102, + "id": "03933516-348a-43dc-b9ec-5bb3d5ebad1a", + "metadata": {}, + "outputs": [], + "source": [ + "SLL.reverse_k(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "id": "d7710cdc-8ced-4f59-b850-e395c78cdc6c", "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "'not found!'" - ] - }, - "execution_count": 405, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "[5]\n" + ] } ], "source": [ - "SLL.deleteNode(14)" + "print(SLL)" ] }, { "cell_type": "markdown", - "id": "8fb2fa75-a010-4f57-afe0-24e3bd89e5b5", + "id": "843af083-9bc2-4e41-a62c-d6f2e4cbd28a", "metadata": {}, "source": [ - "### circuler singly linked-list" + "### circuler doubly Linked List" ] }, { "cell_type": "code", - "execution_count": 368, - "id": "72a0ee56-ea12-43b2-ab42-bed27ae496fa", + "execution_count": null, + "id": "279c4f18-e02a-4d57-b026-b3a092237bc5", "metadata": {}, "outputs": [], "source": [ - "class node:\n", + "class Node:\n", " def __init__(self,data):\n", + " self.prev=None\n", " self.data=data\n", " self.next=None\n", - "class Csl:\n", + " \n", + "class CDL:\n", " def __init__(self):\n", " self.head=None\n", " self.tail=None\n", - "\n", + " \n", + " def __str__(self):\n", + " a=[]\n", + " n=self.head\n", + " while n:\n", + " a.append(n.data)\n", + " if n.next==self.head:\n", + " break\n", + " n=n.next\n", + " return str(a)\n", + " \n", " def append(self,data):\n", " new_node=Node(data)\n", - " if self.head == None:\n", + " if self.head==None:\n", " self.head=new_node\n", " self.tail=new_node\n", " new_node.next=new_node\n", + " new_node.prev=new_node\n", " return data\n", + " new_node.prev=self.tail\n", + " new_node.next=self.head\n", " self.tail.next=new_node\n", " self.tail=new_node\n", - " new_node.next=self.head\n", " return data\n", " \n", - " def display(self):\n", - " if self.head != None:\n", - " n=self.head\n", - " while True:\n", - " print(n.data, end=' ')\n", - " n=n.next\n", - " if n==self.head:\n", - " break\n", - " \n", - " def detectLoop(self):\n", - " n=self.head\n", - " if n != None:\n", - " while True:\n", - " n=n.next\n", - " if n==self.head:\n", - " return True\n", - " return False\n", " \n", - " def detectLoop2(self):\n", - " if self.head.next is not None:\n", - " slow = self.head\n", - " fast = self.head.next\n", - " while fast and fast.next:\n", - " if(slow == fast):\n", - " return True\n", - " slow = slow.next\n", - " fast = fast.next.next\n", - " return False\n", - " \n", - "CSLL=Csl() " + "cdl=CDL()" ] }, { "cell_type": "code", - "execution_count": 369, - "id": "2626dca3-4f78-4886-b5d3-888f0a74b254", + "execution_count": 145, + "id": "2bf9162e-3276-4988-8be2-082a6f3c01de", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[]\n" + ] + } + ], "source": [ - "CSLL.display()" + "print(cdl)" ] }, { "cell_type": "code", - "execution_count": 370, - "id": "48ed5ff8-6a29-46c4-b30e-01125a127ff3", + "execution_count": 150, + "id": "baefcf7e-f4f9-4330-b572-0cf8e03e686a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "18" + "2" ] }, - "execution_count": 370, + "execution_count": 150, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "CSLL.append(18)" + "cdl.append(2)" ] }, { "cell_type": "code", - "execution_count": 371, - "id": "47b27424-d2cb-49ad-a984-fc8b875c1fa2", + "execution_count": 151, + "id": "36a1a2b1-8f5d-4248-b3ae-17eebf5f81f7", "metadata": {}, "outputs": [ { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 371, - "metadata": {}, - "output_type": "execute_result" + "name": "stdout", + "output_type": "stream", + "text": [ + "[5, 4, 2]\n" + ] + } + ], + "source": [ + "print(cdl)" + ] + }, + { + "cell_type": "markdown", + "id": "972760c7-75c9-4503-8256-4c4af6054553", + "metadata": {}, + "source": [ + "## Interview Question" + ] + }, + { + "cell_type": "markdown", + "id": "c381c4b3-95b1-477d-99e3-5822cbdf5962", + "metadata": {}, + "source": [ + "#### 1. bit manupulation (divide two integer)\n", + "#### 2. Reversal in linked list\n", + "#### 3. cycle detection" + ] + }, + { + "cell_type": "markdown", + "id": "e25fb359-a0a7-4a51-925c-30f94e8289b0", + "metadata": {}, + "source": [ + "### 1. Remove all occurences of duplicates in a linked list\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "05299a81-fd0a-4749-860e-8f8c01c130ff", + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self, data):\n", + " self.data = data\n", + " self.next = None\n", + "\n", + "class LinkedList:\n", + " def __init__(self):\n", + " self.head = None\n", + " self.tail = None\n", + "\n", + " def append(self, data):\n", + " new_node = Node(data)\n", + " if self.head is None:\n", + " self.head = new_node\n", + " self.tail = new_node\n", + " else:\n", + " self.tail.next = new_node\n", + " self.tail = new_node\n", + "\n", + " def print(self):\n", + " current = self.head\n", + " while current:\n", + " print(current.data, end=' ')\n", + " current = current.next\n", + " \n", + " def removeAllDuplicates(self,):\n", + " if not head:\n", + " return None\n", + " \n", + " # Dummy node to handle edge cases\n", + " dummy = Node(0)\n", + " dummy.next = head\n", + " \n", + " # To keep track of the previous node\n", + " prev = dummy\n", + " current = head\n", + " \n", + " while current:\n", + " # Check for duplicates\n", + " if current.next and current.data == current.next.data:\n", + " # Skip all nodes with the same value\n", + " while current.next and current.data == current.next.data:\n", + " current = current.next\n", + " # Link prev node to the node after the duplicates\n", + " prev.next = current.next\n", + " else:\n", + " # No duplicates, move prev to current\n", + " prev = prev.next\n", + " \n", + " # Move current to the next node\n", + " current = current.next\n", + " \n", + " return dummy.next\n", + "\n", + "Sl=LinkedList()" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "a7d12814-3bf5-4e3c-9f49-be3e2f02c04e", + "metadata": {}, + "outputs": [], + "source": [ + "Sl.append(1)" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "9681422d-233a-4cd9-b93e-32d316e85aa2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 " + ] } ], "source": [ - "CSLL.detectLoop()" + "Sl.print()" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "ffff8a23-0368-4d79-a25c-e867469cf2ed", + "metadata": {}, + "outputs": [], + "source": [ + "Sl.removeAllDuplicates()" ] }, { "cell_type": "code", - "execution_count": 372, - "id": "1db193a9-bd50-46a3-8cd0-bd21471d0cb0", + "execution_count": 102, + "id": "5b14ab86-4bb1-42aa-9dfa-5dbb8b1354d6", + "metadata": {}, + "outputs": [], + "source": [ + "class Node:\n", + " def __init__(self,val):\n", + " self.data=val\n", + " self.next=None\n", + " self.prev=None\n", + " \n", + "class Dll:\n", + " def __init__(self):\n", + " self.head=None\n", + "\n", + " def addfirst(self,val):\n", + " new_node=Node(val)\n", + " if self.head==None:\n", + " self.head=new_node\n", + " return val\n", + " self.head.prev=new_node\n", + " new_node.next=self.head\n", + " self.head=new_node\n", + " return val\n", + " \n", + " def display(self):\n", + " n=self.head\n", + " while n:\n", + " print(n.data, end=' ')\n", + " n=n.next\n", + " \n", + " def reverse(self):\n", + " curr=self.head\n", + " while curr:\n", + " curr.next,curr.prev=curr.prev,curr.next\n", + " self.head=curr\n", + " curr=curr.prev\n", + " \n", + " \n", + "dll=Dll()" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "id": "85795006-c5b5-49eb-bf9e-7e9531118b54", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "True" + "55" ] }, - "execution_count": 372, + "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "CSLL.detectLoop2()" + "dll.addfirst(55)" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "id": "08b40978-20c9-49d2-802c-be9256a29e80", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "55 15 5 " + ] + } + ], + "source": [ + "dll.display()" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "id": "5fb0b26f-c048-409e-a19a-f27f2b9de264", + "metadata": {}, + "outputs": [], + "source": [ + "dll.reverse()" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "id": "dc09a7c4-5f3a-4d58-ad4f-10fe162adfc1", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5 15 55 " + ] + } + ], + "source": [ + "dll.display()" ] }, { "cell_type": "code", "execution_count": null, - "id": "fa138091-7181-487f-bbf6-41be2eb36660", + "id": "8770b9de-8ba8-4cfb-a044-f097f4e60853", "metadata": {}, "outputs": [], "source": [] diff --git a/Slinked_list.py b/LINKED-LIST problems/Slinked_list.py similarity index 100% rename from Slinked_list.py rename to LINKED-LIST problems/Slinked_list.py diff --git a/Linked List Problems .ipynb b/Linked List Problems .ipynb deleted file mode 100644 index f661f6b8..00000000 --- a/Linked List Problems .ipynb +++ /dev/null @@ -1,3944 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "db004f4f-7bb9-4123-a9e5-c2c6b15661c0", - "metadata": {}, - "source": [ - "# Create linked-list Data Structure" - ] - }, - { - "cell_type": "markdown", - "id": "56bd4c12-0755-4e82-81b6-d956030582f5", - "metadata": {}, - "source": [ - "1. **Create a singly linked list:** Implement a class or structure for a singly linked list, including methods to add nodes, remove nodes, and print the list.\n", - "\n", - "2. **Insertion at the beginning:** Write a function to insert a node at the beginning of a singly linked list.\n", - "\n", - "3. **Insertion at the end:** Implement a function to insert a node at the end of a singly linked list.\n", - "\n", - "4. **Insertion at a given position:** Write a function to insert a node at a specified position in a singly linked list.\n", - "\n", - "5. **Deletion at the beginning:** Implement a function to delete a node from the beginning of a singly linked list.\n", - "\n", - "6. **Deletion at the end:** Write a function to delete a node from the end of a singly linked list.\n", - "7. **Deletion at a given position:** Implement a function to delete a node from a specified position in a singly linked list.\n", - "\n", - "8. **Search operation:** Write a function to search for a given value in a singly linked list.\n", - "\n", - "9. **Reverse a singly linked list:** Implement a function to reverse a singly linked list.\n", - "\n", - "10. **Detect loop in a linked list:** Write a function to detect if a loop exists in a singly linked list.\n", - "\n", - "11. **Find the middle of a linked list:** Implement a function to find the middle node of a singly linked list.\n", - "\n", - "12. **Merge two sorted linked lists:** Write a function to merge two sorted singly linked lists into a single sorted linked list.\n", - "\n", - "13. **Remove duplicates from a linked list:** Implement a function to remove duplicates from an unsorted singly linked list.\n", - "\n", - "14. **Check if a linked list is palindrome:** Write a function to check if a singly linked list is a palindrome.\n", - "\n", - "15. **Intersection point of two linked lists:** Implement a function to find the intersection point of two singly linked lists.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 507, - "id": "3867c096-c8c4-4e53-8273-838d9efc262b", - "metadata": {}, - "outputs": [], - "source": [ - "class Node:\n", - " def __init__(self,data):\n", - " self.data=data\n", - " self.next=None\n", - " \n", - "class SinglyLL:\n", - " def __init__(self):\n", - " self.head=None\n", - "\n", - " def insert_begin(self,data):\n", - " new_node=Node(data)\n", - " if self.head is None:\n", - " self.head=new_node\n", - " return\n", - " new_node.next=self.head\n", - " self.head=new_node\n", - "\n", - " def insert_end(self,data):\n", - " new_node=Node(data)\n", - " if self.head is None:\n", - " self.head=new_node\n", - " return\n", - " n=self.head\n", - " while n.next is not None:\n", - " n=n.next\n", - " n.next=new_node\n", - " \n", - " def _print(self):\n", - " if self.head is None:\n", - " return \"linked list is Empty!\"\n", - " else:\n", - " n=self.head\n", - " while n is not None:\n", - " print(n.data, end=' ')\n", - " n=n.next\n", - " print()\n", - " \n", - " def Remove_begin(self):\n", - " if self.head is None:\n", - " return \"linked list is Empty!\"\n", - " elif self.head.next is None:\n", - " self.head=None\n", - " else:\n", - " self.head=self.head.next\n", - " \n", - " def Remove_end(self):\n", - " if self.head is None:\n", - " return \"SLL is Empty!\"\n", - " elif self.head.next is None:\n", - " self.head=None\n", - " else:\n", - " n=self.head\n", - " while n.next.next is not None:\n", - " n=n.next\n", - " n.next=None\n", - " \n", - " def insert_after_node(self,x,data):\n", - " new_node=Node(data)\n", - " if self.head is None:\n", - " return \"SLL is Empty!\"\n", - " if self.head.data==x:\n", - " new_node.next=self.head.next\n", - " self.head.next=new_node\n", - " return\n", - " n=self.head\n", - " while n is not None:\n", - " if n.data==x:\n", - " new_node.next=n.next\n", - " n.next=new_node\n", - " return\n", - " n=n.next\n", - " return f\"Node with value {x} not found in the list.\"\n", - "\n", - " def insert_before_node(self,x,data):\n", - " new_node=Node(data)\n", - " if self.head is None:\n", - " return \"SLL is Empty!\"\n", - " if self.head.data==x:\n", - " new_node.next=self.head\n", - " self.head=new_node\n", - " return\n", - " n=self.head\n", - " while n.next is not None:\n", - " if n.next.data==x:\n", - " new_node.next=n.next\n", - " n.next=new_node\n", - " return\n", - " n=n.next\n", - " return f\"Node with value {x} not found in the list.\"\n", - " \n", - "SLL=SinglyLL()" - ] - }, - { - "cell_type": "code", - "execution_count": 517, - "id": "6e249fc5-17bd-4e03-9693-0691ad0a813d", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "12 12 128 15 \n" - ] - } - ], - "source": [ - "SLL._print()" - ] - }, - { - "cell_type": "code", - "execution_count": 509, - "id": "09587c81-80b0-4224-9357-e053e8e8d488", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'SLL is Empty!'" - ] - }, - "execution_count": 509, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "SLL.insert_after_node(10,13)" - ] - }, - { - "cell_type": "code", - "execution_count": 510, - "id": "37b110d0-8607-41f6-895f-6c0636a8731a", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'SLL is Empty!'" - ] - }, - "execution_count": 510, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "SLL.Remove_end()" - ] - }, - { - "cell_type": "code", - "execution_count": 511, - "id": "0670e0c5-45ca-404f-9aae-e7e4affe26da", - "metadata": {}, - "outputs": [], - "source": [ - "SLL.insert_begin(15)" - ] - }, - { - "cell_type": "code", - "execution_count": 516, - "id": "3f2e28f0-edbd-480b-b891-a0a02b5247dd", - "metadata": {}, - "outputs": [], - "source": [ - "SLL.insert_before_node(15,128)" - ] - }, - { - "cell_type": "code", - "execution_count": 77, - "id": "fe1df777-6b75-4810-9732-c954bfea83f4", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'linked list is Empty!'" - ] - }, - "execution_count": 77, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "SLL.Remove_begin()" - ] - }, - { - "cell_type": "code", - "execution_count": 602, - "id": "af0dc5d9-5fad-4058-9a6b-7d08c0f9371c", - "metadata": {}, - "outputs": [], - "source": [ - "from collections import deque" - ] - }, - { - "cell_type": "markdown", - "id": "d53003ea-140d-47e6-b1ac-b3aa098daa47", - "metadata": {}, - "source": [ - "7. **Deletion at a given position:** Implement a function to delete a node from a specified position in a singly linked list.\n", - "\n", - "8. **Search operation:** Write a function to search for a given value in a singly linked list.\n", - "\n", - "9. **Reverse a singly linked list:** Implement a function to reverse a singly linked list.\n", - "\n", - "10. **Detect loop in a linked list:** Write a function to detect if a loop exists in a singly linked list.\n", - "\n", - "11. **Find the middle of a linked list:** Implement a function to find the middle node of a singly linked list.\n", - "\n", - "12. **Merge two sorted linked lists:** Write a function to merge two sorted singly linked lists into a single sorted linked list.\n", - "\n", - "13. **Remove duplicates from a linked list:** Implement a function to remove duplicates from an unsorted singly linked list.\n", - "\n", - "14. **Check if a linked list is palindrome:** Write a function to check if a singly linked list is a palindrome.\n", - "\n", - "15. **Intersection point of two linked lists:** Implement a function to find the intersection point of two singly linked lists.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 1092, - "id": "5d8f32c4-d4b7-43f2-b758-e8000391660e", - "metadata": {}, - "outputs": [], - "source": [ - "class Node:\n", - " def __init__(self,data):\n", - " self.data=data\n", - " self.next=None\n", - " \n", - "class SinglyLL:\n", - " def __init__(self):\n", - " self.head=None\n", - "\n", - " def insert_begin(self,data):\n", - " new_node=Node(data)\n", - " if self.head is None:\n", - " self.head=new_node\n", - " return\n", - " new_node.next=self.head\n", - " self.head=new_node\n", - " \n", - " def insert_end(self,data):\n", - " new_node=Node(data)\n", - " if self.head is None:\n", - " self.head=new_node\n", - " return\n", - " n=self.head\n", - " while n.next is not None:\n", - " n=n.next\n", - " n.next=new_node\n", - " \n", - " def _print(self):\n", - " if self.head is None:\n", - " return \"linked list is Empty!\"\n", - " else:\n", - " n=self.head\n", - " while n is not None:\n", - " print(n.data, end=' ')\n", - " n=n.next\n", - " print()\n", - " \n", - " def Delete_by_value(self,x):\n", - " if self.head is None:\n", - " return \"SLL is Empty!\"\n", - " if self.head.data==x:\n", - " self.head=self.head.next\n", - " return\n", - " n=self.head\n", - " while n.next is not None:\n", - " if n.next.data==x:\n", - " n.next=n.next.next\n", - " return\n", - " return \"Given Node is not present in SLL\"\n", - " \n", - " def Search_node(self,x):\n", - " if self.head is None:\n", - " return \"SLL is Empty!\"\n", - " if self.head.data==x:\n", - " return f\"Given node: {x} is present in SLL!\"\n", - " n=self.head\n", - " while n.next is not None:\n", - " if n.next.data==x:\n", - " return f\"Given node: {x} is present in SLL!\"\n", - " n=n.next\n", - " return f\"Given node: {x} is not present in SLL!\"\n", - " \n", - " def Remove_end(self):\n", - " if self.head is None:\n", - " return \"SLL is Empty!\"\n", - " elif self.head.next is None:\n", - " self.head=None\n", - " else:\n", - " n=self.head\n", - " while n.next.next is not None:\n", - " n=n.next\n", - " n.next=None\n", - " \n", - " def length(self):\n", - " if self.head is None:\n", - " return \"SLL is Empty!\"\n", - " n=self.head\n", - " l=0\n", - " while n.next is not None:\n", - " l +=1\n", - " n=n.next\n", - " return l+1\n", - " \n", - " def middle_of_SLL(self):\n", - " if self.head is None:\n", - " return \"SLL is Empty!\"\n", - " k=self.length()\n", - " m=k//2\n", - " n=self.head\n", - " for _ in range(m):\n", - " n=n.next\n", - " print(n.data, end=' ')\n", - " \n", - " def Bubble_sort_SLL(self):\n", - " if self.head is None or self.head.next is None:\n", - " return \"SLL has no Node or only one Node!\"\n", - " length = self.length()\n", - " for i in range(length - 1):\n", - " n = self.head# https://leetcode.com/problems/double-a-number-represented-as-a-linked-list/description/\n", - "\n", - "# Definition for singly-linked list.\n", - "class ListNode:\n", - " def __init__(self, val=0, next=None):\n", - " self.val = val\n", - " self.next = next\n", - "class Solution:\n", - " def __init__(self) -> None:\n", - " self.head=None\n", - "\n", - " def insert(self,x):\n", - " new_node=ListNode(x)\n", - " new_node.next=self.head\n", - " self.head=new_node\n", - "\n", - " def print(self):\n", - " if self.head is not None:\n", - " n=self.head\n", - " while n:\n", - " print(n.val,end=' ')\n", - " n=n.next\n", - "\n", - " def clear(self):\n", - " self.head=None\n", - "\n", - " def doubleIt(self):\n", - " list=[]\n", - " n=self.head\n", - " while n:\n", - " a.append(n.val)\n", - " n=n.next\n", - " self.clear()\n", - " result = [int(''.join(map(str, list)))]\n", - " for i in result:\n", - " a = i * 2\n", - " result1 = [int(digit) for digit in str(a)]\n", - " for i in result1:\n", - " self.insert(i)\n", - " \n", - "\n", - " \n", - "if __name__ == '__main__':\n", - " s=Solution()\n", - " s.insert(3)\n", - " s.insert(2)\n", - " s.insert(1)\n", - " print()\n", - " s.print()\n", - " s.doubleIt()\n", - " s.print\n", - "\n", - "\n", - "\n", - " \n", - " for j in range(length - i - 1):\n", - " if n.data > n.next.data:\n", - " n.data, n.next.data = n.next.data, n.data\n", - " n = n.next\n", - " return self._print()\n", - " \n", - " \n", - " def Removeduplicates_SLL(self):\n", - " if self.head is None or self.head.next is None:\n", - " return \"SLL is Empty or Only One Node!\"\n", - " n=self.head\n", - " unique=set()\n", - " while n is not None:\n", - " unique.add(n.data)\n", - " n=n.next\n", - " s=list(unique)\n", - " for i in range(len(s)):\n", - " print(s[i],end=' ')\n", - " \n", - " def palindrome_SLL(self):\n", - " if self.head is None:\n", - " return \"SLL is Empty!\"\n", - " n=self.head\n", - " unique=list()\n", - " while n is not None:\n", - " unique.append(n.data)\n", - " n=n.next\n", - " s=unique\n", - " if s==s[::-1]:\n", - " return \"This is palindrome SLL!\"\n", - " return \"This is Note A palindrome SLL!\"\n", - " \n", - "\n", - "sll=SinglyLL()" - ] - }, - { - "cell_type": "code", - "execution_count": 1101, - "id": "d6b2d059-21a4-4834-94b0-60115e48124d", - "metadata": {}, - "outputs": [], - "source": [ - "sll.insert_begin(7)" - ] - }, - { - "cell_type": "code", - "execution_count": 1102, - "id": "dd6be5ce-88b8-4caf-b8af-04ffca9cbf9c", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'This is Note A palindrome SLL!'" - ] - }, - "execution_count": 1102, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sll.palindrome_SLL()" - ] - }, - { - "cell_type": "code", - "execution_count": 1103, - "id": "f26d790b-e027-46c5-aed7-55383f0f05a2", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "7 9 9 9 \n" - ] - } - ], - "source": [ - "sll._print()" - ] - }, - { - "cell_type": "code", - "execution_count": 1063, - "id": "6d78e8c4-eb46-44a4-b249-5f71c2c822e9", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'SLL has no Node or only one Node!'" - ] - }, - "execution_count": 1063, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sll.Bubble_sort_SLL()" - ] - }, - { - "cell_type": "code", - "execution_count": 1064, - "id": "53ea7884-ca28-40bc-8c39-a1d0e6431284", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 1064, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sll.length()" - ] - }, - { - "cell_type": "code", - "execution_count": 1072, - "id": "07700dc7-9b80-4e0a-96ff-83e57bb9ab9f", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "8 5 7 " - ] - } - ], - "source": [ - "sll.Removeduplicates_SLL()" - ] - }, - { - "cell_type": "code", - "execution_count": 1033, - "id": "c9d706bf-c108-457a-b412-7472836120f5", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "9 " - ] - } - ], - "source": [ - "sll.middle_of_SLL()" - ] - }, - { - "cell_type": "code", - "execution_count": 859, - "id": "2e345fe5-642c-417d-a4ff-057e1cfacb49", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Given Node is not present in SLL'" - ] - }, - "execution_count": 859, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sll.Delete_by_value(1)" - ] - }, - { - "cell_type": "code", - "execution_count": 655, - "id": "3fd0ef59-0d9f-4c13-83a4-5adedabf4d54", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Given node: 7 is not present in SLL!'" - ] - }, - "execution_count": 655, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sll.Search_node(7)" - ] - }, - { - "cell_type": "code", - "execution_count": 656, - "id": "890e344f-0ec2-41fa-b9bf-b7cb08fe4a5c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1 \n" - ] - } - ], - "source": [ - "sll._print()" - ] - }, - { - "cell_type": "code", - "execution_count": 783, - "id": "320abd28-1db3-481d-8241-6a8174438ddb", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[23, 23, 23, 24, 433]" - ] - }, - "execution_count": 783, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "def bubble(arr):\n", - " for i in range(len(arr)-1):\n", - " for j in range(len(arr)):\n", - " if arr[i]>arr[i+1]:\n", - " arr[i],arr[i+1]=arr[i+1],arr[i]\n", - "A=[23,433,23,23,24]\n", - "bubble(A)\n", - "A" - ] - }, - { - "cell_type": "markdown", - "id": "139e94cf-5fe3-478d-8d10-98d4e196c92b", - "metadata": {}, - "source": [ - "# 1. 🔄 Reverse a Linked List" - ] - }, - { - "cell_type": "code", - "execution_count": 160, - "id": "cbf0862b-cb14-4e6b-b7b8-8e7dcf761972", - "metadata": {}, - "outputs": [], - "source": [ - "class Node:\n", - " def __init__(self,data):\n", - " self.data=data\n", - " self.next=None\n", - " \n", - "class SinglyLL:\n", - " def __init__(self):\n", - " self.head=None\n", - "\n", - " def insert_begin(self,data):\n", - " new_node=Node(data)\n", - " new_node.next=self.head\n", - " self.head=new_node\n", - " \n", - " def _print(self):\n", - " if self.head is None:\n", - " return \"linked list is Empty!\"\n", - " else:\n", - " n=self.head\n", - " while n is not None:\n", - " print(n.data, end=' ')\n", - " n=n.next\n", - " \n", - " def Remove_begin(self):\n", - " if self.head is None:\n", - " return \"linked list is Empty!\"\n", - " elif self.head.next is None:\n", - " self.head=None\n", - " else:\n", - " self.head=self.head.next\n", - " \n", - " def Reverse_SLL(self):\n", - " if self.head is None:\n", - " return \"Linked list is Empty!\"\n", - " \n", - " current = self.head\n", - " previous = None\n", - " while current is not None:\n", - " next_node = current.next\n", - " current.next = previous\n", - " previous = current\n", - " current = next_node\n", - " self.head = previous\n", - " current = self.head\n", - " while current is not None:\n", - " print(current.data, end=\" \")\n", - " current = current.next\n", - "RSLL=SinglyLL()" - ] - }, - { - "cell_type": "code", - "execution_count": 161, - "id": "9268ef31-9ae1-4462-a342-9beab892b091", - "metadata": {}, - "outputs": [], - "source": [ - "RSLL.insert_begin(1)\n", - "RSLL.insert_begin(2)\n", - "RSLL.insert_begin(3)\n", - "RSLL.insert_begin(4)\n", - "RSLL.insert_begin(5)\n", - "RSLL.insert_begin(6)" - ] - }, - { - "cell_type": "code", - "execution_count": 162, - "id": "3a7a4c96-edab-4c9a-865d-05b946b384f1", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "6 5 4 3 2 1 " - ] - } - ], - "source": [ - "RSLL._print()" - ] - }, - { - "cell_type": "code", - "execution_count": 163, - "id": "c00666ee-a325-42f7-9daf-ab1b8a57922c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1 2 3 4 5 6 " - ] - } - ], - "source": [ - "RSLL.Reverse_SLL()" - ] - }, - { - "cell_type": "markdown", - "id": "98c18bda-c37c-469e-b1dc-e53d975b2fd6", - "metadata": {}, - "source": [ - "# 2. 🔁 Detect a Cycle in a Linked List\n" - ] - }, - { - "cell_type": "code", - "execution_count": 381, - "id": "d566949a-f98e-49ad-a4a2-2f9075d87bc5", - "metadata": {}, - "outputs": [], - "source": [ - "class Node:\n", - " def __init__(self, data):\n", - " self.prev = None\n", - " self.data = data\n", - " self.next = None\n", - "\n", - "class CDLL:\n", - " def __init__(self):\n", - " self.head = None\n", - " \n", - " def _print(self):\n", - " if self.head is None:\n", - " return \"CDLL is Empty!\"\n", - " if self.head is not None:\n", - " n=self.head\n", - " while n!=self.head.prev:\n", - " print(n.data, end=' ')\n", - " n=n.next\n", - " print(n.data, end=' ')\n", - " \n", - " def insert_begin(self, data):\n", - " new_node=Node(data)\n", - " if self.head is None:\n", - " self.head=new_node\n", - " new_node.next=new_node\n", - " new_node.prev=new_node\n", - " else:\n", - " last_node=self.head.prev\n", - " new_node.prev=self.head.prev\n", - " new_node.next=self.head\n", - " self.head.prev=new_node\n", - " last_node.next=new_node\n", - " \n", - " def detect_cycle(self):\n", - " if self.head is None:\n", - " return \"CDLL is Empty!\"\n", - " \n", - " while self.head.next is not None and self.head.next.next is not None:\n", - " if self.head == self.head.next:\n", - " return \"Cycle detected!\"\n", - " self.head = self.head.next\n", - " self.head.next = self.head.next.next.next\n", - "\n", - " else:\n", - " return \"Cycle not detected!\"\n", - " \n", - "\n", - "CDll=CDLL()" - ] - }, - { - "cell_type": "code", - "execution_count": 379, - "id": "418e3908-bde8-478a-8042-0f2fb2d32999", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1 1 1 " - ] - } - ], - "source": [ - "CDll._print()" - ] - }, - { - "cell_type": "code", - "execution_count": 378, - "id": "d19b6a67-fc86-4c79-bdba-4a0ce8923f65", - "metadata": {}, - "outputs": [], - "source": [ - "CDll.insert_begin(1)" - ] - }, - { - "cell_type": "code", - "execution_count": 380, - "id": "7267894f-8a10-4f16-a671-a725dd01599e", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'Cycle detected!'" - ] - }, - "execution_count": 380, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "CDll.detect_cycle()" - ] - }, - { - "cell_type": "markdown", - "id": "7dae4818-def0-4e6f-9197-08cfcecdba6e", - "metadata": {}, - "source": [ - "\n", - "# 3. 🤝 Find the Merge Point of Two Linked Lists\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "47e15829-fbd2-44d7-8f3c-576382b76e8d", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "8450f872-977e-4484-99bd-7e4d9a93af66", - "metadata": {}, - "source": [ - "\n", - "# 4. 🚫 Remove N-th Node From End of List\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9e1ad243-1aac-4184-83c2-74d383fbd6b6", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "845ea2ad-0810-48ad-b1d4-740fddb77674", - "metadata": {}, - "source": [ - "\n", - "# 5. 🔗 Merge Two Sorted Linked Lists\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "bcb40684-4c82-47bd-aba4-e0e00d8cfd79", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "36abf3d4-7063-4d75-ac31-1e59c1e474a4", - "metadata": {}, - "source": [ - "\n", - "# 6. 🖼️ Check if a Linked List is a Palindrome\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5a56d1ae-e814-4387-871e-182a62595959", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "24b06d25-8872-4b70-b101-8250330a039e", - "metadata": {}, - "source": [ - "\n", - "# 7. 🚨 Remove Duplicates from a Sorted List\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3758f388-6237-49e9-91c8-8d7726fb65a4", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "13aaf7dd-3b30-490f-af06-193356d38784", - "metadata": {}, - "source": [ - "\n", - "# 8. 🎯 Find the Middle of a Linked List\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "ee0e005b-0906-4276-a48d-710d513b0c9a", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "18e53811-0fd4-40da-9cc2-e41c4df98a8b", - "metadata": {}, - "source": [ - "\n", - "# 9. 🔄 Rotate a Linked List\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "9c5142d9-cf81-4207-8a3d-7df5427e9cdd", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "9121b674-5fa5-459d-81f1-770c45bdf816", - "metadata": {}, - "source": [ - "\n", - "# 10. 📑 Implement a Doubly Linked List\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "4ee2a457-a57e-4270-9069-c75c47086672", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "364f2c2a-86af-4b19-9821-400a64d85893", - "metadata": {}, - "source": [ - "\n", - "# 11. 📊 Implement a Circular Linked List\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "fb778604-b4e2-4b56-bad2-6a6f5d01415d", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "67e03aa6-b593-4e64-b112-84340a85c639", - "metadata": {}, - "source": [ - "\n", - "# 12. 🛠️ Add Two Numbers Represented by Linked Lists\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "66392f56-a2c2-47f0-8b4e-eaebf6c0b5eb", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "6094fcf9-feaa-4dde-9b16-e364daa711fe", - "metadata": {}, - "source": [ - "\n", - "# 13. 🧹 Remove Linked List Elements\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "854d93b7-361c-4ede-9447-2ae4a04b923a", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "0c0a1ae1-49fd-4171-98c7-edf2da446f85", - "metadata": {}, - "source": [ - "\n", - "# 14. 🧩 Partition List around a value" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "794c5a6b-0fc1-47ca-93da-42772b64eef9", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "224b2b38-80ab-4b90-8686-63a53c2c641d", - "metadata": {}, - "source": [ - "\n", - "\n", - "# 15. 🔄 Reverse Nodes in k-Group" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0fcbb10a-5cc1-4e7a-b523-27e0917962a8", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "markdown", - "id": "fcc07c89-ecf8-44e0-8796-dcf719e587ad", - "metadata": {}, - "source": [ - "**complexities of various operations on a linked list:**\n", - "| Operation | Time Complexity |\n", - "|-------------------------------|-----------------|\n", - "| Insertion at Beginning | \\(O(1)\\) |\n", - "| Insertion at End | \\(O(n)\\) |\n", - "| Insertion at Specific Position| \\(O(n)\\) |\n", - "| Deletion at Beginning | \\(O(1)\\) |\n", - "| Deletion at End | \\(O(n)\\) |\n", - "| Deletion at Specific Position | \\(O(n)\\) |\n", - "| Search | \\(O(n)\\) |\n", - "| Access/Traversal | \\(O(n)\\) |\n" - ] - }, - { - "cell_type": "code", - "execution_count": 77, - "id": "42556b24-ce59-4ac5-8b73-816426442f98", - "metadata": {}, - "outputs": [], - "source": [ - "class Node:\n", - " def __init__(self,data):\n", - " self.data=data\n", - " self.next=None\n", - " \n", - "class SynglyLL:\n", - " def __init__(self):\n", - " self.head=None\n", - "\n", - " def len(self):\n", - " if self.head is None: return 0\n", - " count=0\n", - " n=self.head\n", - " while n:\n", - " n=n.next\n", - " count+=1\n", - " return count\n", - " \n", - " def insert_begin(self,data):\n", - " new_node=Node(data)\n", - " new_node.next=self.head\n", - " self.head=new_node\n", - " return\n", - " \n", - " def print(self):\n", - " if self.head is None: return \"SLL is Empty!\"\n", - " n=self.head\n", - " while n:\n", - " print(n.data, end= ' ')\n", - " n=n.next\n", - " \n", - " def insert_end(self,data):\n", - " new_node=Node(data)\n", - " if self.head is None:\n", - " self.insert_begin(data)\n", - " return\n", - " n=self.head\n", - " while n.next:\n", - " n=n.next\n", - " n.next=new_node\n", - " \n", - " def insert_by_value(self,data,x):\n", - " new_node=Node(data)\n", - " if self.head is None: return \"Empty! LL!\"\n", - " n=self.head\n", - " while n:\n", - " if n.data==x:\n", - " new_node.next=n.next\n", - " n.next=new_node\n", - " return\n", - " n=n.next\n", - " print(f\"{x} Node is not present in LL!\")\n", - " \n", - " def insert_mid(self,data):\n", - " new_node=Node(data)\n", - " if self.head is None:\n", - " self.insert_begin(data)\n", - " return\n", - " mid=self.len()//2\n", - " n=self.len()\n", - " if n%2==1:\n", - " mid +=1\n", - " n=self.head\n", - " while mid>1:\n", - " n=n.next\n", - " mid -=1\n", - " new_node.next=n.next\n", - " n.next=new_node\n", - " \n", - " def insert_after(self,data,x):\n", - " new_node=Node(data)\n", - " n=self.head\n", - " while n:\n", - " if n.data==x:\n", - " new_node.next=n.next\n", - " n.next=new_node\n", - " return\n", - " n=n.next\n", - " print(f\"{x} node is not present in SLL or SLL is Empty!\") \n", - "\n", - " def clear(self):\n", - " self.head=None\n", - " \n", - " def remove_begin(self):\n", - " if self.head is None:\n", - " raise IndexError (\"Empty! LL\")\n", - " return\n", - " self.head=self.head.next\n", - " \n", - " def remove_end(self):\n", - " if self.head is None:\n", - " raise IndexError (\"Empty! LL\")\n", - " return\n", - " if self.head.next is None:\n", - " self.head=None\n", - " return\n", - " n=self.head\n", - " while n.next.next:\n", - " n=n.next\n", - " n.next=None\n", - " \n", - " def delete_Node(self,x):\n", - " if self.head is None:\n", - " raise IndexError (\"Empty! LL\")\n", - " return\n", - " if self.head.data==x:\n", - " self.head=self.head.next\n", - " return\n", - " n=self.head\n", - " while n:\n", - " if n.data==x:\n", - " n.next=n.next.next\n", - " n=n.next\n", - " print(f\"{x} node is not present in SLL!\")\n", - "\n", - " def search(self,x):\n", - " if self.head is None:\n", - " raise IndexError (\"Empty! LL\")\n", - " return\n", - " n=self.head\n", - " count=0\n", - " while n:\n", - " if n.data==x:\n", - " print(f\"{x} is present at index {count}\")\n", - " return\n", - " count+=1\n", - " n=n.next\n", - " print(f\"{x} node is not present in SLL!\")\n", - "\n", - " def search_by_index(self,x):\n", - " if self.head is None:\n", - " raise IndexError (\"Empty! LL\")\n", - " return\n", - " if x>self.len():\n", - " raise IndexError (\"Given Index is out Of range!\")\n", - " return\n", - " if x==0:\n", - " return self.head.data\n", - " n=self.head\n", - " while x!=0:\n", - " x -=1\n", - " n=n.next \n", - " print(n.data) \n", - " \n", - " def delete_by_index(self,x):\n", - " if self.head is None:\n", - " raise IndexError (\"Empty! LL\")\n", - " return\n", - " if x>=self.len():\n", - " raise IndexError (\"Given Index is out Of range!\")\n", - " return\n", - " if x==0:\n", - " self.head=self.head.next\n", - " return\n", - " if x==self.len()-1:\n", - " self.remove_end()\n", - " return \n", - " n = self.head\n", - " while x != 1:\n", - " n = n.next\n", - " x -= 1\n", - " n.next = n.next.next\n", - " \n", - " def Reverse(self):\n", - " if self.head is None:\n", - " raise IndexError (\"Empty! LL\")\n", - " return\n", - " if self.head.next is None:\n", - " self.head.data\n", - " return \n", - " cur=self.head\n", - " prev=None\n", - " while cur:\n", - " temp=cur.next\n", - " cur.next=prev\n", - " prev=cur\n", - " cur=temp\n", - " self.head=prev\n", - "\n", - " def mid_of_sll(self):\n", - " if self.head is None:\n", - " raise IndexError(\"Empty! LL\")\n", - " \n", - " mid = self.len() // 2\n", - " if self.len() % 2 == 1:\n", - " mid += 1\n", - " \n", - " n = self.head\n", - " while mid != 0:\n", - " n = n.next\n", - " mid -= 1\n", - " \n", - " return n.data\n", - "\n", - " \n", - " def Reverse_k_element(self, k):\n", - " if self.head is None:\n", - " raise IndexError(\"Empty! LL\")\n", - " return\n", - " if k>self.len() or k<=0:\n", - " raise IndexError (\"Given Index is out Of range!\")\n", - " return\n", - " n=self.head\n", - " prev=None\n", - " \n", - " def replace_max(self,data):\n", - " if self.head is None:\n", - " raise IndexError(\"Empty! LL\")\n", - " return\n", - " n=self.head\n", - " max=n\n", - " while n:\n", - " if n.data>max.data:\n", - " max= n\n", - " n=n.next\n", - " max.data=data\n", - " \n", - " def get_odd_index_value(self):\n", - " if self.head is None:\n", - " return 0\n", - " \n", - " n = self.head\n", - " cur = 0\n", - " result = 0\n", - " \n", - " while n:\n", - " if cur % 2 != 0:\n", - " result += n.data\n", - " cur += 1\n", - " n = n.next\n", - " \n", - " return result\n", - " \n", - " \n", - " \n", - "Sll=SynglyLL() " - ] - }, - { - "cell_type": "code", - "execution_count": 80, - "id": "7a643579-fe84-42c5-a189-8cd36ecaae8c", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "22" - ] - }, - "execution_count": 80, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Sll.get_odd_index_value()" - ] - }, - { - "cell_type": "code", - "execution_count": 79, - "id": "1ac15268-d411-4fae-a6fc-3888a6a673aa", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "20 1 3 20 1 1 " - ] - } - ], - "source": [ - "Sll.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 66, - "id": "6096ff7d-48a1-4d09-85e9-cf657136a693", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.replace_max(3)" - ] - }, - { - "cell_type": "code", - "execution_count": 648, - "id": "5c6317f1-a3c5-4565-9146-2221186e5954", - "metadata": {}, - "outputs": [], - "source": [ - "# Sll.Reverse_k_element(0)" - ] - }, - { - "cell_type": "code", - "execution_count": 706, - "id": "32ebe16b-8b4d-4f59-b696-64be4a3d3414", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "30" - ] - }, - "execution_count": 706, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Sll.mid_of_sll()" - ] - }, - { - "cell_type": "code", - "execution_count": 78, - "id": "7b784e82-3ad2-43dd-9340-3b3d4a354715", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.insert_begin(1)\n", - "Sll.insert_begin(1)\n", - "Sll.insert_begin(20)\n", - "Sll.insert_begin(3)\n", - "Sll.insert_begin(1)\n", - "Sll.insert_begin(20)\n" - ] - }, - { - "cell_type": "code", - "execution_count": 608, - "id": "cc1b7d23-bddb-4af8-8b8f-41f370a0a19d", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.Reverse()" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "id": "94aea40c-decd-4ac6-97d9-8d0e784b8d5f", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3 1 1 " - ] - } - ], - "source": [ - "Sll.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "id": "edc0e050-3159-4625-b444-60d19fa69913", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "20 10 30 " - ] - } - ], - "source": [ - "Sll.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 572, - "id": "af374a0c-384f-4635-8192-43ecf44b0adc", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.delete_by_index(1)" - ] - }, - { - "cell_type": "code", - "execution_count": 594, - "id": "a1a4bb68-8ab5-4d57-8121-74883ad05df4", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "23 2 4 3 " - ] - } - ], - "source": [ - "Sll.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "77c91097-7a5f-4d6b-bc4d-0e3eae828ab7", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "20 10 30 " - ] - } - ], - "source": [ - "Sll.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 700, - "id": "93f906b8-a1cc-4cf8-b5ba-0f882cb689ee", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.insert_mid(23)\n", - "Sll.insert_mid(3)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "6ea3d0c4-f377-4b09-b57b-2ed56954197d", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5ea956cb-e2fb-434c-a4ce-14b1b9e865b4", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 375, - "id": "d686fed1-4912-4a9a-a305-9a0ebab476d2", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3\n" - ] - } - ], - "source": [ - "Sll.search_by_index(3)" - ] - }, - { - "cell_type": "code", - "execution_count": 576, - "id": "77682589-fbe9-46ad-b1d2-2cb0d3ad0bdf", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "23 " - ] - } - ], - "source": [ - "Sll.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 469, - "id": "46e082db-99dc-43bb-9a32-7878bcf4bb67", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 469, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Sll.len()" - ] - }, - { - "cell_type": "code", - "execution_count": 368, - "id": "1d02d05e-0ba4-4b4c-8f2d-82358710d62f", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.insert_mid(23)\n", - "Sll.insert_mid(3)\n", - "Sll.insert_mid(2)\n", - "Sll.insert_mid(4)" - ] - }, - { - "cell_type": "code", - "execution_count": 225, - "id": "04ab3ea3-614a-4a80-bec9-0980f49631ae", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2 is present at index 1\n" - ] - } - ], - "source": [ - "Sll.search(2)" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3ba7e7a2-0523-4b6b-a368-3b72556e3521", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 168, - "id": "59f22499-e967-44c9-aabb-acb2bc0b49f4", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.delete_Node(23)" - ] - }, - { - "cell_type": "code", - "execution_count": 268, - "id": "8bf98093-7348-48c3-ae22-2f26418461de", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "23 3 23 2 " - ] - } - ], - "source": [ - "Sll.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 163, - "id": "752d1e91-fd3e-49a1-a05b-10fc0aa05f0c", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.remove_end()" - ] - }, - { - "cell_type": "code", - "execution_count": 223, - "id": "60a05ba7-b240-4b6b-8482-985c00d99db1", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 194, - "id": "d135b2b2-3432-4c50-a0f1-af380aaf5372", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "23 node is not present in SLL or SLL is Empty!\n" - ] - } - ], - "source": [ - "Sll.insert_after(0,23)" - ] - }, - { - "cell_type": "code", - "execution_count": 110, - "id": "cc603b46-4f8e-47aa-ae49-17e779bd9cdb", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 110, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Sll.len()" - ] - }, - { - "cell_type": "code", - "execution_count": 129, - "id": "3ab47c1b-3535-4800-91c9-c17a7acd02b7", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'SLL is Empty!'" - ] - }, - "execution_count": 129, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Sll.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 663, - "id": "248f88f4-c111-472d-a582-52a6018ed4ec", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.clear()" - ] - }, - { - "cell_type": "code", - "execution_count": 551, - "id": "3b9a59ca-b19d-4feb-acd0-812c423b8580", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.insert_begin(30)" - ] - }, - { - "cell_type": "code", - "execution_count": 184, - "id": "1afa28d4-46bd-46ff-b493-12f7552bc94e", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "14 14 " - ] - }, - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 184, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Sll.print()\n", - "Sll.len()" - ] - }, - { - "cell_type": "code", - "execution_count": 556, - "id": "b1c580f6-4d86-49fb-b30b-f5d4a89b6abc", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.insert_end(11)" - ] - }, - { - "cell_type": "code", - "execution_count": 170, - "id": "0e2aa244-7b46-4300-9b5e-abe2d6f94a60", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "21 Node is not present in LL!\n" - ] - } - ], - "source": [ - "Sll.insert_by_value(25,21)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "7f673360-4dd5-4184-a62a-18eba6a4d8ad", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "3eb3fd97-5ceb-48bb-a12c-263e4d16735c", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 405, - "id": "724d0353-a06f-41ec-ac65-0a8c48e4ae67", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "27" - ] - }, - "execution_count": 405, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "b=53\n", - "result = b // 2 # This will result in 1\n", - "if b % 2 == 1: # Check if there's a remainder\n", - " result += 1 # Add 1 to the result\n", - "result" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f1f940d7-f7ad-495d-9ab8-4d7ab2d5ac81", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 426, - "id": "9440974a-c712-41d9-bf62-f37c169dce9e", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "26" - ] - }, - "execution_count": 426, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "mid=53//2\n", - "if mid % 2 == 1: # Check if there's a remainder\n", - " mid += 1 # Add 1 to the result\n", - "mid" - ] - }, - { - "cell_type": "code", - "execution_count": 421, - "id": "ca2f4d66-7820-49da-b926-ad5367290144", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "1" - ] - }, - "execution_count": 421, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "3//2" - ] - }, - { - "cell_type": "code", - "execution_count": 451, - "id": "5ece34a5-449f-4df6-8a0f-72f2cb3f1448", - "metadata": {}, - "outputs": [], - "source": [ - "n=53//2\n", - "m = 53 # This results in m = 26\n", - "if m % 2 == 1: # Checks if m is odd\n", - " n += 1 # Increments m by 1 if it's odd" - ] - }, - { - "cell_type": "code", - "execution_count": 452, - "id": "ba114919-8760-48d7-9f4f-98a4b1d71238", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "27" - ] - }, - "execution_count": 452, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "n" - ] - }, - { - "cell_type": "code", - "execution_count": 448, - "id": "c0190adf-4a01-4d79-a9f6-81cfc3db8a9e", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "26" - ] - }, - "execution_count": 448, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "m" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "id": "5120a887-08f7-4606-90f0-a5ae487a6552", - "metadata": {}, - "outputs": [], - "source": [ - "class Node:\n", - " def __init__(self,data):\n", - " self.data=data\n", - " self.next=None\n", - " \n", - "class SinglyLL:\n", - " def __init__(self):\n", - " self.head=None\n", - "\n", - " def insert_begin(self,data):\n", - " new_node=Node(data)\n", - " if self.head is None:\n", - " self.head=new_node\n", - " return\n", - " new_node.next=self.head\n", - " self.head=new_node\n", - "\n", - " def insert_end(self,data):\n", - " new_node=Node(data)\n", - " if self.head is None:\n", - " self.head=new_node\n", - " return\n", - " n=self.head\n", - " while n.next is not None:\n", - " n=n.next\n", - " n.next=new_node\n", - " \n", - " def _print(self):\n", - " if self.head is None:\n", - " return \"linked list is Empty!\"\n", - " else:\n", - " n=self.head\n", - " while n is not None:\n", - " print(n.data, end=' ')\n", - " n=n.next\n", - " print()\n", - " \n", - " def len(self):\n", - " if self.head is None: return 0\n", - " n=self.head\n", - " count=0\n", - " while n:\n", - " count+=1\n", - " n=n.next\n", - " return count\n", - " \n", - " \n", - " def Reverse(self):\n", - " prev=None\n", - " curr=self.head\n", - " for i in range(self.len()):\n", - " temp=curr.next\n", - " curr.next=prev\n", - " prev=curr\n", - " curr=temp\n", - " self.head=prev\n", - " \n", - " \n", - "sll=SinglyLL()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "0c846d35-fdd3-4b8e-9944-268adfeca393", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "73555790-8d4c-4099-a2b6-b7b09d1f9da5", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "2f9886f8-6213-4967-9284-dd17c2e1f77c", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "d3b0111f-7911-4d12-8820-fdc684ddc0ec", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 46, - "id": "8fbb9b97-777d-4d4f-b9e1-78268d058e31", - "metadata": {}, - "outputs": [], - "source": [ - "sll.insert_begin(1)\n", - "sll.insert_begin(2)\n", - "sll.insert_begin(3)\n", - "sll.insert_begin(4)\n", - "sll.insert_begin(5)" - ] - }, - { - "cell_type": "code", - "execution_count": 47, - "id": "c5f1e179-53fb-419c-abb2-ecfcb4137942", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5 4 3 2 1 1 2 3 4 5 \n" - ] - } - ], - "source": [ - "sll._print()" - ] - }, - { - "cell_type": "code", - "execution_count": 48, - "id": "2a8c3f9a-6159-42a2-8b64-94e71a1d0a8f", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "10" - ] - }, - "execution_count": 48, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sll.len()" - ] - }, - { - "cell_type": "code", - "execution_count": 49, - "id": "869761fd-7980-4852-9bff-12d0fe5d7bf7", - "metadata": {}, - "outputs": [], - "source": [ - "sll.Reverse()" - ] - }, - { - "cell_type": "code", - "execution_count": 50, - "id": "585d75ee-2fb6-4368-8d7e-152e615faeca", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5 4 3 2 1 1 2 3 4 5 \n" - ] - } - ], - "source": [ - "sll._print()" - ] - }, - { - "cell_type": "markdown", - "id": "e6356f84-7b6e-400d-bca0-7ee0726ddb50", - "metadata": {}, - "source": [ - "## **Problem Statement:**\n", - "\n", - "Given a singly linked list containing characters, write a Python function to return a new string created by appending all the characters from the linked list following these rules:\n", - "\n", - "1. Replace '*' or '/' with a single space.\n", - "2. If there are two consecutive occurrences of '*' or '/', replace those occurrences with a single space and convert the next character to uppercase.\n", - " \n", - "Assumptions:\n", - "\n", - "- There will not be more than two consecutive occurrences of '*' or '/'.\n", - "- The linked list will always end with an alphabet.\n", - "\n", - "**Example:**\n", - "\n", - "Input Linked List: A, n, *, /, a, p, ., p, l, e, *, a, /, d, a, y, *, *, k, e, e, p, s, ., /, ., *, a, /, /, d, o, c, t, o, r, *, A, w, a, y\n", - "\n", - "Output: \"AN APPLE A DAY KEEPS AWAY\"" - ] - }, - { - "cell_type": "code", - "execution_count": 100, - "id": "5e2c752f-dc87-44af-a408-71ba4eea7fae", - "metadata": {}, - "outputs": [], - "source": [ - "class Node:\n", - " def __init__(self,data):\n", - " self.data=data\n", - " self.next=None\n", - " \n", - "class SinglyLL:\n", - " def __init__(self):\n", - " self.head=None\n", - "\n", - " def insert_begin(self,data):\n", - " new_node=Node(data)\n", - " if self.head is None:\n", - " self.head=new_node\n", - " return\n", - " new_node.next=self.head\n", - " self.head=new_node\n", - "\n", - " def insert_end(self,data):\n", - " new_node=Node(data)\n", - " if self.head is None:\n", - " self.head=new_node\n", - " return\n", - " n=self.head\n", - " while n.next is not None:\n", - " n=n.next\n", - " n.next=new_node\n", - " \n", - " def _print(self):\n", - " if self.head is None:\n", - " return \"linked list is Empty!\"\n", - " else:\n", - " n=self.head\n", - " while n is not None:\n", - " print(n.data, end='')\n", - " n=n.next\n", - " print()\n", - " \n", - " def len(self):\n", - " if self.head is None: return 0\n", - " n=self.head\n", - " count=0\n", - " while n:\n", - " count+=1\n", - " n=n.next\n", - " return count\n", - " \n", - " \n", - " def Reverse(self):\n", - " prev=None\n", - " curr=self.head\n", - " for i in range(self.len()):\n", - " temp=curr.next\n", - " curr.next=prev\n", - " prev=curr\n", - " curr=temp\n", - " self.head=prev\n", - " \n", - " def change_sentence(self):\n", - " if self.head is None: return \"Empty LL!\"\n", - " n= self.head\n", - " while n:\n", - " if n.data== \"*\" or n.data==\"/\":\n", - " n.data=\" \"\n", - " if n.next.data== \"*\" or n.next.data== \"/\":\n", - " n.next.next.data=n.next.next.data.upper()\n", - " n.next=n.next.next\n", - " n=n.next\n", - " \n", - "Sl=SinglyLL()" - ] - }, - { - "cell_type": "code", - "execution_count": 101, - "id": "ad5f6f83-4460-4630-a68f-18bc2e910cef", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'linked list is Empty!'" - ] - }, - "execution_count": 101, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Sl._print()" - ] - }, - { - "cell_type": "code", - "execution_count": 102, - "id": "3b08210c-d854-4551-879a-4dd6180d14fa", - "metadata": {}, - "outputs": [], - "source": [ - "Sl.insert_end('A')\n", - "Sl.insert_end('n')\n", - "Sl.insert_end('*')\n", - "Sl.insert_end('/')\n", - "Sl.insert_end('a')\n", - "Sl.insert_end('p')\n", - "Sl.insert_end('p')\n", - "Sl.insert_end('l')\n", - "Sl.insert_end('e')\n", - "Sl.insert_end('*')\n", - "Sl.insert_end('a')\n", - "Sl.insert_end('/')\n", - "Sl.insert_end('d')\n", - "Sl.insert_end('a')\n", - "Sl.insert_end('y')\n", - "Sl.insert_end('*')\n", - "Sl.insert_end('*')\n", - "Sl.insert_end('k')\n", - "Sl.insert_end('e')\n", - "Sl.insert_end('e')\n", - "Sl.insert_end('p')\n", - "Sl.insert_end('s')\n", - "Sl.insert_end('/')\n", - "Sl.insert_end('*')\n", - "Sl.insert_end('a')\n", - "Sl.insert_end('/')\n", - "Sl.insert_end('/')\n", - "Sl.insert_end('d')\n", - "Sl.insert_end('o')\n", - "Sl.insert_end('c')\n", - "Sl.insert_end('t')\n", - "Sl.insert_end('o')\n", - "Sl.insert_end('r')\n", - "Sl.insert_end('*')\n", - "Sl.insert_end('A')\n", - "Sl.insert_end('w')\n", - "Sl.insert_end('a')\n", - "Sl.insert_end('y')" - ] - }, - { - "cell_type": "code", - "execution_count": 103, - "id": "1f4e88a0-bd2c-41b0-947f-fc46158021f6", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "An*/apple*a/day**keeps/*a//doctor*Away\n" - ] - } - ], - "source": [ - "Sl._print()" - ] - }, - { - "cell_type": "code", - "execution_count": 104, - "id": "f1271771-181d-49da-959b-2855a43674be", - "metadata": {}, - "outputs": [], - "source": [ - "Sl.change_sentence()" - ] - }, - { - "cell_type": "code", - "execution_count": 105, - "id": "d3d804f6-7c27-4740-b8a6-922009ac7c32", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "An Apple a day Keeps A Doctor Away\n" - ] - } - ], - "source": [ - "Sl._print()" - ] - }, - { - "cell_type": "code", - "execution_count": 100, - "id": "6101b892-75d0-491c-a1fa-6d72f2ec3a0d", - "metadata": {}, - "outputs": [], - "source": [ - "# https://leetcode.com/problems/double-a-number-represented-as-a-linked-list/description/\n", - "\n", - "# Definition for singly-linked list.\n", - "class ListNode:\n", - " def __init__(self, val=0, next=None):\n", - " self.val = val\n", - " self.next = next\n", - "class Solution:\n", - " def __init__(self) -> None:\n", - " self.head=None\n", - "\n", - " def insert(self,x):\n", - " new_node=ListNode(x)\n", - " new_node.next=self.head\n", - " self.head=new_node\n", - "\n", - " def print(self):\n", - " if self.head is not None:\n", - " n=self.head\n", - " while n:\n", - " print(n.val,end=' ')\n", - " n=n.next\n", - "\n", - " def clear(self):\n", - " self.head=None\n", - "\n", - " def doubleIt(self):\n", - " list=[]\n", - " n=self.head\n", - " while n:\n", - " list.append(n.val)\n", - " n=n.next\n", - " self.clear()\n", - " result = [int(''.join(map(str, list)))]\n", - " for i in result:\n", - " a = i * 2\n", - " result1 = [int(digit) for digit in str(a)]\n", - " reult1=result1[::-1]\n", - " for i in result1:\n", - " self.insert(i)\n", - " return self.print()\n", - " \n", - "\n", - "\n", - "\n", - "s=Solution()" - ] - }, - { - "cell_type": "code", - "execution_count": 104, - "id": "43e17bc9-099f-4f5a-bcfc-3b1f63428564", - "metadata": {}, - "outputs": [], - "source": [ - "s.insert(13)" - ] - }, - { - "cell_type": "code", - "execution_count": 105, - "id": "0aeca022-0635-4d07-9bc6-deccbdacf325", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "13 6 " - ] - } - ], - "source": [ - "s.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 106, - "id": "984dd3a0-46f9-409a-bb8b-fca6fca304b6", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "2 7 2 " - ] - } - ], - "source": [ - "s.doubleIt()" - ] - }, - { - "cell_type": "code", - "execution_count": 183, - "id": "90965987-2e8a-4085-a63e-a6c4f766d3ae", - "metadata": {}, - "outputs": [], - "source": [ - "# https://www.geeksforgeeks.org/reverse-a-linked-list/\n", - "\n", - "class Node:\n", - " def __init__(self, data):\n", - " self.data = data\n", - " self.next = None\n", - "\n", - "class LinkedList:\n", - " def __init__(self):\n", - " self.head = None\n", - " self.tail = None\n", - "\n", - " def insert_begin(self, data):\n", - " new_node = Node(data)\n", - " if self.head is None:\n", - " self.head = new_node\n", - " self.tail = new_node\n", - " else:\n", - " new_node.next = self.head\n", - " self.head = new_node\n", - "\n", - " def insert_end(self, data):\n", - " new_node = Node(data)\n", - " if self.head is None:\n", - " self.head = new_node\n", - " self.tail = new_node\n", - " else:\n", - " self.tail.next = new_node\n", - " self.tail = new_node\n", - "\n", - " def print(self):\n", - " current = self.head\n", - " while current:\n", - " print(current.data, end=' ')\n", - " current = current.next\n", - " print()\n", - " \n", - " def Reverse(self):\n", - " if self.head is None:\n", - " return \"LL Empty!\"\n", - " current = self.head\n", - " prev = None\n", - " while current is not None:\n", - " next_node = current.next\n", - " current.next = prev\n", - " prev = current\n", - " current = next_node\n", - "\n", - " self.head = prev\n", - " return prev\n", - "\n", - " def rotate(self, k):\n", - " pass\n", - " \n", - " def clear(self):\n", - " self.head=None\n", - "\n", - "S=LinkedList()" - ] - }, - { - "cell_type": "code", - "execution_count": 171, - "id": "2197c32d-c770-48b8-829b-b7fa7f688d3d", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], - "source": [ - "S.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 186, - "id": "21542d1a-b4aa-4a8c-ad4b-3e401c3c235d", - "metadata": {}, - "outputs": [], - "source": [ - "S.insert_begin(14)" - ] - }, - { - "cell_type": "code", - "execution_count": 187, - "id": "73f997d4-c344-4b6e-8747-5c4c6669a2e8", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "14 4 \n" - ] - } - ], - "source": [ - "S.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 188, - "id": "fb3a9753-4a13-414f-bf4e-bcbd4a32e911", - "metadata": {}, - "outputs": [], - "source": [ - "S.insert_end(17)" - ] - }, - { - "cell_type": "code", - "execution_count": 189, - "id": "1eb243ae-78c2-412c-81f8-324817a70e53", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "14 4 17 \n" - ] - } - ], - "source": [ - "S.print()" - ] - }, - { - "cell_type": "markdown", - "id": "c912cf69-a3cc-4ac7-94c3-a01c4a72b475", - "metadata": {}, - "source": [ - "1. **Create a singly linked list:** Implement a class or structure for a singly linked list, including methods to add nodes, remove nodes, and print the list.\n", - "\n", - "2. **Insertion at the beginning:** Write a function to insert a node at the beginning of a singly linked list.\n", - "\n", - "3. **Insertion at the end:** Implement a function to insert a node at the end of a singly linked list.\n", - "\n", - "4. **Insertion at a given position:** Write a function to insert a node at a specified position in a singly linked list.\n", - "\n", - "5. **Deletion at the beginning:** Implement a function to delete a node from the beginning of a singly linked list.\n", - "\n", - "6. **Deletion at the end:** Write a function to delete a node from the end of a singly linked list.\n", - "7. **Deletion at a given position:** Implement a function to delete a node from a specified position in a singly linked list.\n", - "\n", - "8. **Search operation:** Write a function to search for a given value in a singly linked list.\n", - "\n", - "9. **Reverse a singly linked list:** Implement a function to reverse a singly linked list.\n", - "\n", - "10. **Detect loop in a linked list:** Write a function to detect if a loop exists in a singly linked list.\n", - "\n", - "11. **Find the middle of a linked list:** Implement a function to find the middle node of a singly linked list.\n", - "\n", - "12. **Merge two sorted linked lists:** Write a function to merge two sorted singly linked lists into a single sorted linked list.\n", - "\n", - "13. **Remove duplicates from a linked list:** Implement a function to remove duplicates from an unsorted singly linked list.\n", - "\n", - "14. **Check if a linked list is palindrome:** Write a function to check if a singly linked list is a palindrome.\n", - "\n", - "15. **Intersection point of two linked lists:** Implement a function to find the intersection point of two singly linked lists.\n" - ] - }, - { - "cell_type": "code", - "execution_count": 601, - "id": "1625cb39-fd93-494b-8be3-dddca3629da8", - "metadata": {}, - "outputs": [], - "source": [ - "class Node:\n", - " def __init__(self,data):\n", - " self.data=data\n", - " self.next=None\n", - "class Sll:\n", - " def __init__(self):\n", - " self.head=None\n", - " self.tail=None\n", - " \n", - " def append(self,data):\n", - " new_node=Node(data)\n", - " if self.head==None or self.head.data >=data:\n", - " new_node.next=self.head\n", - " self.head=new_node\n", - " self.tail=new_node\n", - " return data\n", - " n=self.head\n", - " while n.next is not None and n.next.data < data:\n", - " n = n.next\n", - " new_node.next=n.next\n", - " n.next=new_node\n", - " if new_node.next is None:\n", - " self.tail = new_node\n", - " \n", - " def display(self):\n", - " if self.head !=None:\n", - " n=self.head\n", - " while n:\n", - " print(n.data,end=' ')\n", - " n=n.next\n", - " if n == self.head:\n", - " break\n", - " \n", - " def reverse(self):\n", - " curr=self.head\n", - " prev=None\n", - " self.tail = self.head\n", - " while curr:\n", - " temp=curr.next\n", - " curr.next=prev\n", - " prev=curr\n", - " curr=temp\n", - " self.head=prev\n", - " #148. Sort List leetcode\n", - " \n", - " def _split_list(self, head):\n", - " if head is None or head.next is None:\n", - " return head, None\n", - " slow = head\n", - " fast = head.next\n", - "\n", - " while fast and fast.next:\n", - " slow = slow.next\n", - " fast = fast.next.next\n", - "\n", - " middle = slow.next\n", - " slow.next = None\n", - "\n", - " return head, middle\n", - "\n", - " def _split_list(self, head):\n", - " if head is None or head.next is None:\n", - " return head, None\n", - " slow = head\n", - " fast = head.next\n", - "\n", - " while fast and fast.next:\n", - " slow = slow.next\n", - " fast = fast.next.next\n", - "\n", - " middle = slow.next\n", - " slow.next = None\n", - "\n", - " return head, middle\n", - "\n", - " def _merge_sorted_lists(self, left, right):\n", - " dummy = Node(0)\n", - " tail = dummy\n", - " while left and right:\n", - " if left.data <= right.data:\n", - " tail.next = left\n", - " left = left.next\n", - " else:\n", - " tail.next = right\n", - " right = right.next\n", - " tail = tail.next\n", - " tail.next = left if left else right\n", - " return dummy.next\n", - "\n", - " def _merge_sort(self, head):\n", - " if head is None or head.next is None:\n", - " return head\n", - " left, right = self._split_list(head)\n", - " left = self._merge_sort(left)\n", - " right = self._merge_sort(right)\n", - " return self._merge_sorted_lists(left, right)\n", - " def sort(self):\n", - " self.head = self._merge_sort(self.head)\n", - " \n", - " def detecte_loop(self):\n", - " fast=self.head\n", - " slow=self.head\n", - " while fast and fast.next:\n", - " slow=slow.next\n", - " fast=fast.next.next\n", - " if slow==fast:\n", - " return True\n", - " return False\n", - " \n", - " def appendleft(self, data):\n", - " new_node = Node(data)\n", - " if self.head is None: # If the list is empty\n", - " self.head = new_node\n", - " new_node.next = self.head\n", - " self.tail = new_node\n", - " return data\n", - " new_node.next=self.head\n", - " self.tail.next=new_node\n", - " self.head=new_node\n", - " return data\n", - " \n", - " def Search (self,x):\n", - " n=self.head\n", - " if n !=None:\n", - " while n:\n", - " if n.data==x:\n", - " return True\n", - " n=n.next\n", - " return False\n", - " \n", - " def middle(self):\n", - " length=0\n", - " n=self.head\n", - " while n:\n", - " length+=1\n", - " n=n.next\n", - " mid=length//2\n", - " n=self.head\n", - " while mid!=0:\n", - " n=n.next\n", - " mid-=1\n", - " return n.data\n", - " \n", - " #Deletion at a given position\n", - " def delete_i(self, x):\n", - " if x == 0:\n", - " if self.head is not None:\n", - " self.head = self.head.next\n", - " return\n", - " n = self.head\n", - " while n is not None and x > 1:\n", - " n = n.next\n", - " x -= 1\n", - " if n is None or n.next is None:\n", - " return\n", - " n.next = n.next.next\n", - " \n", - " \n", - "S1=Sll()" - ] - }, - { - "cell_type": "code", - "execution_count": 602, - "id": "a838f823-249b-42b6-ba19-2d6f9612ef8a", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "9" - ] - }, - "execution_count": 602, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "S1.append(9)" - ] - }, - { - "cell_type": "code", - "execution_count": 603, - "id": "8e05494f-2829-4c9d-8876-417fd61fcb30", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "9 " - ] - } - ], - "source": [ - "S1.display()" - ] - }, - { - "cell_type": "code", - "execution_count": 604, - "id": "1a4112dd-aeee-4ef9-8842-a040af12c2da", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "9" - ] - }, - "execution_count": 604, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "S1.middle()" - ] - }, - { - "cell_type": "code", - "execution_count": 605, - "id": "58110af7-7567-448a-90d9-72416b190d64", - "metadata": {}, - "outputs": [], - "source": [ - "S1.delete_i(0)" - ] - }, - { - "cell_type": "code", - "execution_count": 606, - "id": "5ac74765-b144-467c-8acd-f670a75ca961", - "metadata": {}, - "outputs": [], - "source": [ - "S1.display()" - ] - }, - { - "cell_type": "code", - "execution_count": 1028, - "id": "16f497f2-1edd-4e74-a9f7-edf0ef6f9367", - "metadata": {}, - "outputs": [], - "source": [ - "class Node:\n", - " def __init__(self, data):\n", - " self.data = data\n", - " self.next = None\n", - "\n", - "class LinkedList:\n", - " def __init__(self):\n", - " self.head = None\n", - " self.tail = None\n", - "\n", - " def append(self, data):\n", - " new_node = Node(data)\n", - " if self.head is None:\n", - " self.head = new_node\n", - " self.tail = new_node\n", - " else:\n", - " self.tail.next = new_node\n", - " self.tail = new_node\n", - "\n", - " def print(self):\n", - " current = self.head\n", - " while current:\n", - " print(current.data, end=' ')\n", - " current = current.next\n", - " \n", - " def Remove_ele(self,char):\n", - " try:\n", - " x=self.head.data\n", - " if self.head.data==char:\n", - " self.head=self.head.next\n", - " return x\n", - " n=self.head\n", - " while n:\n", - " if n.next.data==char:\n", - " x=n.next.data\n", - " n.next=n.next.next\n", - " return x\n", - " n=n.next\n", - " return -1\n", - " except:\n", - " return -1\n", - " \n", - " def Remove_similer_element(self,char):\n", - " while self.head and self.head.data == char:\n", - " self.head = self.head.next\n", - "\n", - " current = self.head\n", - " while current and current.next:\n", - " if current.next.data == char:\n", - " current.next = current.next.next\n", - " else:\n", - " current = current.next\n", - "\n", - " return\n", - "\n", - " \n", - " \n", - "sll=LinkedList()" - ] - }, - { - "cell_type": "code", - "execution_count": 1029, - "id": "858c58cf-e528-4dad-97d5-83dcceb635de", - "metadata": {}, - "outputs": [], - "source": [ - "a=\"cccccccccccccdr\"\n", - "for i in a:\n", - " sll.append(i)\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 1030, - "id": "35c119ea-41be-47b9-bcab-e97ea60105c4", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "c c c c c c c c c c c c c d r " - ] - } - ], - "source": [ - "sll.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 1031, - "id": "1858d0c4-9738-4c81-8b63-10272ac955ec", - "metadata": {}, - "outputs": [], - "source": [ - "sll.Remove_similer_element(\"d\")" - ] - }, - { - "cell_type": "code", - "execution_count": 1032, - "id": "86f7ba1e-2e62-4907-b357-eb43795f91ed", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "c c c c c c c c c c c c c r " - ] - } - ], - "source": [ - "sll.print()" - ] - }, - { - "cell_type": "markdown", - "id": "b2af8314-d1a6-4fad-af03-271fc377dfa9", - "metadata": {}, - "source": [ - "| | Question |\n", - "|-------|-------------------------------------------------------------------------------------------------|\n", - "| 1 | Write a Program to reverse the Linked List. (Both Iterative and recursive) |\n", - "| 2 | Reverse a Linked List in group of Given Size. [Very Imp] |\n", - "| 3 | Write a program to Detect loop in a linked list. |\n", - "| 4 | Write a program to Delete loop in a linked list. |\n", - "| 5 | Find the starting point of the loop. |\n", - "| 6 | Remove Duplicates in a sorted Linked List. |\n", - "| 7 | Remove Duplicates in a Un-sorted Linked List. |\n", - "| 8 | Write a Program to Move the last element to Front in a Linked List. |\n", - "| 9 | Add “1” to a number represented as a Linked List. |\n", - "| 10 | Add two numbers represented by linked lists. |\n", - "| 11 | Intersection of two Sorted Linked List. |\n", - "| 12 | Intersection Point of two Linked Lists. |\n", - "| 13 | Merge Sort For Linked lists.[Very Important] |\n", - "| 14 | Quicksort for Linked Lists.[Very Important] |\n", - "| 15 | Find the middle Element of a linked list. |\n", - "| 16 | Check if a linked list is a circular linked list. |\n", - "| 17 | Split a Circular linked list into two halves. |\n", - "| 18 | Write a Program to check whether the Singly Linked list is a palindrome or not. |\n", - "| 19 | Deletion from a Circular Linked List. |\n", - "| 20 | Reverse a Doubly Linked list. |\n", - "| 21 | Find pairs with a given sum in a DLL. |\n", - "| 22 | Count triplets in a sorted DLL whose sum is equal to given value “X”. |\n", - "| 23 | Sort a “k”sorted Doubly Linked list.[Very IMP] |\n", - "| 24 | Rotate DoublyLinked list by N nodes. |\n", - "| 25 | Rotate a Doubly Linked list in group of Given Size.[Very IMP] |\n", - "| 26 | Can we reverse a linked list in less than O(n) ? |\n", - "| 27 | Why Quicksort is preferred for. Arrays and Merge Sort for LinkedLists ? |\n", - "| 28 | Flatten a Linked List |\n", - "| 29 | Sort a LL of 0's, 1's and 2's |\n", - "| 30 | Clone a linked list with next and random pointer |\n", - "| 31 | Merge K sorted Linked list |\n", - "| 32 | Multiply 2 no. represented by LL |\n", - "| 33 | Delete nodes which have a greater value on right side |\n", - "| 34 | Segregate even and odd nodes in a Linked List |\n", - "| 35 | Program for n’th node from the end of a Linked List |\n", - "| 36 | Find the first non-repeating character from a stream of characters |" - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "id": "00b3ebff-a26e-48bd-85ff-5fed6d1e42d9", - "metadata": {}, - "outputs": [], - "source": [ - "import Slinked_list as s\n", - "Sll=s.LinkedList()" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "f23081da-6eca-4bd0-a1b3-3590fcad045b", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.append(8)" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "id": "6558339c-577d-4e14-8b9d-551bb6250e51", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "8 " - ] - } - ], - "source": [ - "Sll.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "id": "c42b3377-c23e-487f-97be-0b6779564d3b", - "metadata": {}, - "outputs": [], - "source": [ - "def clear(self):\n", - " self.head = None\n", - "s.LinkedList.clear = clear" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "id": "3209bd23-1025-4412-a4c9-190bae71348d", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.clear()" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "59febb67-6054-461a-942f-4142b2521558", - "metadata": {}, - "outputs": [], - "source": [ - "# reverse the Linked List. (Both Iterative and recursive)\n", - "def reverse(self):\n", - " n=self.head\n", - " prev=None\n", - " while n:\n", - " temp=n.next\n", - " n.next=prev\n", - " prev=n\n", - " n=temp\n", - " self.head=prev\n", - "s.LinkedList.reverse = reverse" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "77333d3f-d9d6-4d0d-802f-e40ff305cd74", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.reverse()" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "f91957fe-169a-46ba-bceb-1c19851eb16f", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 43, - "id": "539949ea-a062-4141-9860-222f2b65a223", - "metadata": {}, - "outputs": [], - "source": [ - "Sl=s.LinkedList" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "3e063050-9488-4dd3-8676-77285d2c18d1", - "metadata": {}, - "outputs": [], - "source": [ - "# recursive\n", - "def _reverse_recursive(self, current, prev):\n", - " if not current:\n", - " return prev\n", - " next_node = current.next\n", - " current.next = prev\n", - " return self._reverse_recursive(next_node, current)\n", - "\n", - "def reverse2(self):\n", - " self.head = self._reverse_recursive(self.head, None)" - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "id": "7385e51f-72c3-41a7-a641-6add0090198f", - "metadata": {}, - "outputs": [], - "source": [ - "Sl.reverse2=reverse2" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "id": "9e4b89fd-b4b5-40ee-a31e-2dee6f9b0d3b", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "id": "132f601c-21ef-4ca5-a2b5-6f242aece27d", - "metadata": {}, - "outputs": [], - "source": [ - "#Write a program to Detect loop in a linked list.\n", - "def detect_loop(self):\n", - " n=self.head\n", - " try:\n", - " if self.head.next==self.head:\n", - " return True\n", - " while n:\n", - " if n.next==self.head:\n", - " return True\n", - " n=n.next\n", - " except:\n", - " return False\n", - "Sl.detect_loop=detect_loop" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "id": "4e4a9714-eabe-43ef-8365-34bb0590a9d6", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "False" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Sll.detect_loop()" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "id": "75b1704a-d696-4ef2-a9ec-1557a6e42272", - "metadata": {}, - "outputs": [], - "source": [ - "def append_circuler(self,data):\n", - " new_node=s.Node(data)\n", - " if self.head ==None:\n", - " self.head=new_node\n", - " self.tail=new_node\n", - " self.tail.next=self.head\n", - " " - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "id": "8a7db87a-748e-43aa-94ee-ea72205cf7eb", - "metadata": {}, - "outputs": [], - "source": [ - "Sl.append_circuler=append_circuler" - ] - }, - { - "cell_type": "code", - "execution_count": 17, - "id": "4b899075-e8f0-4733-adfe-721fa2556388", - "metadata": {}, - "outputs": [], - "source": [ - "Sll.append_circuler(12)" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "id": "82e6a572-0e22-4c2a-8a5a-e73457ead80d", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "True" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "Sll.detect_loop()" - ] - }, - { - "cell_type": "code", - "execution_count": 81, - "id": "0cbbb0ba-ffbf-429a-9ac7-53956c5a5f24", - "metadata": {}, - "outputs": [], - "source": [ - "k=s.Node" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "68dbeef3-2788-4f38-b2be-6e540de90066", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 98, - "id": "f96656c9-736b-40f6-b829-ee19c531d2c1", - "metadata": {}, - "outputs": [], - "source": [ - "class Node:\n", - " def __init__(self,data):\n", - " self.data=data\n", - " self.next=None\n", - "class Sll:\n", - " def __init__(self):\n", - " self.head=None\n", - " self.tail=None\n", - " \n", - " def __len__(self):\n", - " count=0\n", - " n=self.head\n", - " while n:\n", - " count+=1\n", - " n=n.next\n", - " return count\n", - " \n", - " def append(self,data):\n", - " new_node=Node(data)\n", - " if self.head==None:\n", - " self.head=new_node\n", - " self.tail=new_node\n", - " return data\n", - " self.tail.next=new_node\n", - " self.tail=new_node\n", - " return data\n", - " \n", - " def __str__(self):\n", - " nodes=[]\n", - " if self.head !=None:\n", - " n=self.head\n", - " while n!=None:\n", - " nodes.append(n.data)\n", - " if n.next==self.head:\n", - " break\n", - " n=n.next\n", - " return str(nodes)\n", - " \n", - " def reverse_k(self, k):\n", - " if self.head is not None:\n", - " curr = self.head\n", - " temp_head=self.head\n", - " prev = None\n", - " while curr and k:\n", - " temp = curr.next # Store the next node\n", - " curr.next = prev # Reverse the link\n", - " prev = curr # Move prev and curr one step forward\n", - " curr = temp\n", - " k -= 1\n", - " temp_head.next = temp\n", - " self.head = prev # Update the head of the list to the new front\n", - " \n", - " #\tWrite a program to Delete loop in a linked list.\n", - " def delete_loop(self):\n", - " pass\n", - "\n", - "SLL=Sll()" - ] - }, - { - "cell_type": "code", - "execution_count": 99, - "id": "74dced1a-071a-4edf-9946-8008a5157cab", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "0" - ] - }, - "execution_count": 99, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "len(SLL)" - ] - }, - { - "cell_type": "code", - "execution_count": 100, - "id": "2ed513ba-35d5-4944-8989-b83c7f6b8b4c", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "5" - ] - }, - "execution_count": 100, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "SLL.append(5)" - ] - }, - { - "cell_type": "code", - "execution_count": 101, - "id": "daf8c628-d498-44f8-bcec-9f38587fba62", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[5]\n" - ] - } - ], - "source": [ - "print(SLL)" - ] - }, - { - "cell_type": "code", - "execution_count": 102, - "id": "03933516-348a-43dc-b9ec-5bb3d5ebad1a", - "metadata": {}, - "outputs": [], - "source": [ - "SLL.reverse_k(2)" - ] - }, - { - "cell_type": "code", - "execution_count": 103, - "id": "d7710cdc-8ced-4f59-b850-e395c78cdc6c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[5]\n" - ] - } - ], - "source": [ - "print(SLL)" - ] - }, - { - "cell_type": "markdown", - "id": "843af083-9bc2-4e41-a62c-d6f2e4cbd28a", - "metadata": {}, - "source": [ - "### circuler doubly Linked List" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "279c4f18-e02a-4d57-b026-b3a092237bc5", - "metadata": {}, - "outputs": [], - "source": [ - "class Node:\n", - " def __init__(self,data):\n", - " self.prev=None\n", - " self.data=data\n", - " self.next=None\n", - " \n", - "class CDL:\n", - " def __init__(self):\n", - " self.head=None\n", - " self.tail=None\n", - " \n", - " def __str__(self):\n", - " a=[]\n", - " n=self.head\n", - " while n:\n", - " a.append(n.data)\n", - " if n.next==self.head:\n", - " break\n", - " n=n.next\n", - " return str(a)\n", - " \n", - " def append(self,data):\n", - " new_node=Node(data)\n", - " if self.head==None:\n", - " self.head=new_node\n", - " self.tail=new_node\n", - " new_node.next=new_node\n", - " new_node.prev=new_node\n", - " return data\n", - " new_node.prev=self.tail\n", - " new_node.next=self.head\n", - " self.tail.next=new_node\n", - " self.tail=new_node\n", - " return data\n", - " \n", - " \n", - "cdl=CDL()" - ] - }, - { - "cell_type": "code", - "execution_count": 145, - "id": "2bf9162e-3276-4988-8be2-082a6f3c01de", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[]\n" - ] - } - ], - "source": [ - "print(cdl)" - ] - }, - { - "cell_type": "code", - "execution_count": 150, - "id": "baefcf7e-f4f9-4330-b572-0cf8e03e686a", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "2" - ] - }, - "execution_count": 150, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "cdl.append(2)" - ] - }, - { - "cell_type": "code", - "execution_count": 151, - "id": "36a1a2b1-8f5d-4248-b3ae-17eebf5f81f7", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "[5, 4, 2]\n" - ] - } - ], - "source": [ - "print(cdl)" - ] - }, - { - "cell_type": "markdown", - "id": "972760c7-75c9-4503-8256-4c4af6054553", - "metadata": {}, - "source": [ - "## Interview Question" - ] - }, - { - "cell_type": "markdown", - "id": "c381c4b3-95b1-477d-99e3-5822cbdf5962", - "metadata": {}, - "source": [ - "#### 1. bit manupulation (divide two integer)\n", - "#### 2. Reversal in linked list\n", - "#### 3. cycle detection" - ] - }, - { - "cell_type": "markdown", - "id": "e25fb359-a0a7-4a51-925c-30f94e8289b0", - "metadata": {}, - "source": [ - "### 1. Remove all occurences of duplicates in a linked list\n" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "id": "05299a81-fd0a-4749-860e-8f8c01c130ff", - "metadata": {}, - "outputs": [], - "source": [ - "class Node:\n", - " def __init__(self, data):\n", - " self.data = data\n", - " self.next = None\n", - "\n", - "class LinkedList:\n", - " def __init__(self):\n", - " self.head = None\n", - " self.tail = None\n", - "\n", - " def append(self, data):\n", - " new_node = Node(data)\n", - " if self.head is None:\n", - " self.head = new_node\n", - " self.tail = new_node\n", - " else:\n", - " self.tail.next = new_node\n", - " self.tail = new_node\n", - "\n", - " def print(self):\n", - " current = self.head\n", - " while current:\n", - " print(current.data, end=' ')\n", - " current = current.next\n", - " \n", - " def removeAllDuplicates(self,):\n", - " if not head:\n", - " return None\n", - " \n", - " # Dummy node to handle edge cases\n", - " dummy = Node(0)\n", - " dummy.next = head\n", - " \n", - " # To keep track of the previous node\n", - " prev = dummy\n", - " current = head\n", - " \n", - " while current:\n", - " # Check for duplicates\n", - " if current.next and current.data == current.next.data:\n", - " # Skip all nodes with the same value\n", - " while current.next and current.data == current.next.data:\n", - " current = current.next\n", - " # Link prev node to the node after the duplicates\n", - " prev.next = current.next\n", - " else:\n", - " # No duplicates, move prev to current\n", - " prev = prev.next\n", - " \n", - " # Move current to the next node\n", - " current = current.next\n", - " \n", - " return dummy.next\n", - "\n", - "Sl=LinkedList()" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "id": "a7d12814-3bf5-4e3c-9f49-be3e2f02c04e", - "metadata": {}, - "outputs": [], - "source": [ - "Sl.append(1)" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "9681422d-233a-4cd9-b93e-32d316e85aa2", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1 " - ] - } - ], - "source": [ - "Sl.print()" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "id": "ffff8a23-0368-4d79-a25c-e867469cf2ed", - "metadata": {}, - "outputs": [], - "source": [ - "Sl.removeAllDuplicates()" - ] - }, - { - "cell_type": "code", - "execution_count": 102, - "id": "5b14ab86-4bb1-42aa-9dfa-5dbb8b1354d6", - "metadata": {}, - "outputs": [], - "source": [ - "class Node:\n", - " def __init__(self,val):\n", - " self.data=val\n", - " self.next=None\n", - " self.prev=None\n", - " \n", - "class Dll:\n", - " def __init__(self):\n", - " self.head=None\n", - "\n", - " def addfirst(self,val):\n", - " new_node=Node(val)\n", - " if self.head==None:\n", - " self.head=new_node\n", - " return val\n", - " self.head.prev=new_node\n", - " new_node.next=self.head\n", - " self.head=new_node\n", - " return val\n", - " \n", - " def display(self):\n", - " n=self.head\n", - " while n:\n", - " print(n.data, end=' ')\n", - " n=n.next\n", - " \n", - " def reverse(self):\n", - " curr=self.head\n", - " while curr:\n", - " curr.next,curr.prev=curr.prev,curr.next\n", - " self.head=curr\n", - " curr=curr.prev\n", - " \n", - " \n", - "dll=Dll()" - ] - }, - { - "cell_type": "code", - "execution_count": 108, - "id": "85795006-c5b5-49eb-bf9e-7e9531118b54", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "55" - ] - }, - "execution_count": 108, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "dll.addfirst(55)" - ] - }, - { - "cell_type": "code", - "execution_count": 109, - "id": "08b40978-20c9-49d2-802c-be9256a29e80", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "55 15 5 " - ] - } - ], - "source": [ - "dll.display()" - ] - }, - { - "cell_type": "code", - "execution_count": 110, - "id": "5fb0b26f-c048-409e-a19a-f27f2b9de264", - "metadata": {}, - "outputs": [], - "source": [ - "dll.reverse()" - ] - }, - { - "cell_type": "code", - "execution_count": 111, - "id": "dc09a7c4-5f3a-4d58-ad4f-10fe162adfc1", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "5 15 55 " - ] - } - ], - "source": [ - "dll.display()" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "8770b9de-8ba8-4cfb-a044-f097f4e60853", - "metadata": {}, - "outputs": [], - "source": [] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "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.11.7" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} diff --git a/Searching Algorithms/.ipynb_checkpoints/Searching Algorithms-checkpoint.ipynb b/Searching & Sorting/.ipynb_checkpoints/Searching Algorithms-checkpoint.ipynb similarity index 100% rename from Searching Algorithms/.ipynb_checkpoints/Searching Algorithms-checkpoint.ipynb rename to Searching & Sorting/.ipynb_checkpoints/Searching Algorithms-checkpoint.ipynb diff --git a/Searching Algorithms/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/Searching & Sorting/.ipynb_checkpoints/Untitled-checkpoint.ipynb similarity index 100% rename from Searching Algorithms/.ipynb_checkpoints/Untitled-checkpoint.ipynb rename to Searching & Sorting/.ipynb_checkpoints/Untitled-checkpoint.ipynb diff --git a/Searching Algorithms/Searching Algorithms.ipynb b/Searching & Sorting/Searching Algorithms.ipynb similarity index 100% rename from Searching Algorithms/Searching Algorithms.ipynb rename to Searching & Sorting/Searching Algorithms.ipynb diff --git a/Stacks problems/.idea/.gitignore b/Searching & Sorting/Sorting Algorithms/.idea/.gitignore similarity index 100% rename from Stacks problems/.idea/.gitignore rename to Searching & Sorting/Sorting Algorithms/.idea/.gitignore diff --git a/Stacks problems/.idea/Stacks problems.iml b/Searching & Sorting/Sorting Algorithms/.idea/Sorting Algorithms.iml similarity index 100% rename from Stacks problems/.idea/Stacks problems.iml rename to Searching & Sorting/Sorting Algorithms/.idea/Sorting Algorithms.iml diff --git a/Stacks problems/.idea/inspectionProfiles/profiles_settings.xml b/Searching & Sorting/Sorting Algorithms/.idea/inspectionProfiles/profiles_settings.xml similarity index 100% rename from Stacks problems/.idea/inspectionProfiles/profiles_settings.xml rename to Searching & Sorting/Sorting Algorithms/.idea/inspectionProfiles/profiles_settings.xml diff --git a/Stacks problems/.idea/misc.xml b/Searching & Sorting/Sorting Algorithms/.idea/misc.xml similarity index 100% rename from Stacks problems/.idea/misc.xml rename to Searching & Sorting/Sorting Algorithms/.idea/misc.xml diff --git a/Sorting Algorithms/.idea/modules.xml b/Searching & Sorting/Sorting Algorithms/.idea/modules.xml similarity index 100% rename from Sorting Algorithms/.idea/modules.xml rename to Searching & Sorting/Sorting Algorithms/.idea/modules.xml diff --git a/Stacks problems/.idea/vcs.xml b/Searching & Sorting/Sorting Algorithms/.idea/vcs.xml similarity index 100% rename from Stacks problems/.idea/vcs.xml rename to Searching & Sorting/Sorting Algorithms/.idea/vcs.xml diff --git a/Sorting Algorithms/1. Bubble Sort.py b/Searching & Sorting/Sorting Algorithms/1. Bubble Sort.py similarity index 100% rename from Sorting Algorithms/1. Bubble Sort.py rename to Searching & Sorting/Sorting Algorithms/1. Bubble Sort.py diff --git a/Sorting Algorithms/2. selection sort.py b/Searching & Sorting/Sorting Algorithms/2. selection sort.py similarity index 100% rename from Sorting Algorithms/2. selection sort.py rename to Searching & Sorting/Sorting Algorithms/2. selection sort.py diff --git a/Sorting Algorithms/3. insertion sort.py b/Searching & Sorting/Sorting Algorithms/3. insertion sort.py similarity index 100% rename from Sorting Algorithms/3. insertion sort.py rename to Searching & Sorting/Sorting Algorithms/3. insertion sort.py diff --git a/Sorting Algorithms/4. merge sort.py b/Searching & Sorting/Sorting Algorithms/4. merge sort.py similarity index 100% rename from Sorting Algorithms/4. merge sort.py rename to Searching & Sorting/Sorting Algorithms/4. merge sort.py diff --git a/sorting.ipynb b/Searching & Sorting/sorting.ipynb similarity index 100% rename from sorting.ipynb rename to Searching & Sorting/sorting.ipynb diff --git a/Queue Problems .ipynb b/Stack & Queue/Queue Problems .ipynb similarity index 100% rename from Queue Problems .ipynb rename to Stack & Queue/Queue Problems .ipynb diff --git a/Stack roblems.ipynb b/Stack & Queue/Stack roblems.ipynb similarity index 100% rename from Stack roblems.ipynb rename to Stack & Queue/Stack roblems.ipynb diff --git a/Stack & Queue/Stacks problems/.idea/.gitignore b/Stack & Queue/Stacks problems/.idea/.gitignore new file mode 100644 index 00000000..26d33521 --- /dev/null +++ b/Stack & Queue/Stacks problems/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/Stack & Queue/Stacks problems/.idea/Stacks problems.iml b/Stack & Queue/Stacks problems/.idea/Stacks problems.iml new file mode 100644 index 00000000..d0876a78 --- /dev/null +++ b/Stack & Queue/Stacks problems/.idea/Stacks problems.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Stack & Queue/Stacks problems/.idea/inspectionProfiles/profiles_settings.xml b/Stack & Queue/Stacks problems/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000..105ce2da --- /dev/null +++ b/Stack & Queue/Stacks problems/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/Stack & Queue/Stacks problems/.idea/misc.xml b/Stack & Queue/Stacks problems/.idea/misc.xml new file mode 100644 index 00000000..309fdffc --- /dev/null +++ b/Stack & Queue/Stacks problems/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/Stacks problems/.idea/modules.xml b/Stack & Queue/Stacks problems/.idea/modules.xml similarity index 100% rename from Stacks problems/.idea/modules.xml rename to Stack & Queue/Stacks problems/.idea/modules.xml diff --git a/Stack & Queue/Stacks problems/.idea/vcs.xml b/Stack & Queue/Stacks problems/.idea/vcs.xml new file mode 100644 index 00000000..6c0b8635 --- /dev/null +++ b/Stack & Queue/Stacks problems/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Stacks problems/.ipynb_checkpoints/Problems Practice-checkpoint.ipynb b/Stack & Queue/Stacks problems/.ipynb_checkpoints/Problems Practice-checkpoint.ipynb similarity index 100% rename from Stacks problems/.ipynb_checkpoints/Problems Practice-checkpoint.ipynb rename to Stack & Queue/Stacks problems/.ipynb_checkpoints/Problems Practice-checkpoint.ipynb diff --git a/Stacks problems/.ipynb_checkpoints/Stack-checkpoint.ipynb b/Stack & Queue/Stacks problems/.ipynb_checkpoints/Stack-checkpoint.ipynb similarity index 100% rename from Stacks problems/.ipynb_checkpoints/Stack-checkpoint.ipynb rename to Stack & Queue/Stacks problems/.ipynb_checkpoints/Stack-checkpoint.ipynb diff --git a/Stacks problems/.ipynb_checkpoints/Untitled-checkpoint.ipynb b/Stack & Queue/Stacks problems/.ipynb_checkpoints/Untitled-checkpoint.ipynb similarity index 100% rename from Stacks problems/.ipynb_checkpoints/Untitled-checkpoint.ipynb rename to Stack & Queue/Stacks problems/.ipynb_checkpoints/Untitled-checkpoint.ipynb diff --git a/Stacks problems/1. Implement two Stacks in an Array.py b/Stack & Queue/Stacks problems/1. Implement two Stacks in an Array.py similarity index 100% rename from Stacks problems/1. Implement two Stacks in an Array.py rename to Stack & Queue/Stacks problems/1. Implement two Stacks in an Array.py diff --git a/Stacks problems/10. Implement Undo and Redo features of a Text Editor.py b/Stack & Queue/Stacks problems/10. Implement Undo and Redo features of a Text Editor.py similarity index 100% rename from Stacks problems/10. Implement Undo and Redo features of a Text Editor.py rename to Stack & Queue/Stacks problems/10. Implement Undo and Redo features of a Text Editor.py diff --git a/Stacks problems/11. The Celebrity Problem.py b/Stack & Queue/Stacks problems/11. The Celebrity Problem.py similarity index 100% rename from Stacks problems/11. The Celebrity Problem.py rename to Stack & Queue/Stacks problems/11. The Celebrity Problem.py diff --git a/Stacks problems/12 Get minimum element from stack.py b/Stack & Queue/Stacks problems/12 Get minimum element from stack.py similarity index 100% rename from Stacks problems/12 Get minimum element from stack.py rename to Stack & Queue/Stacks problems/12 Get minimum element from stack.py diff --git a/Stacks problems/13 Reverse an array using Stack.py b/Stack & Queue/Stacks problems/13 Reverse an array using Stack.py similarity index 100% rename from Stacks problems/13 Reverse an array using Stack.py rename to Stack & Queue/Stacks problems/13 Reverse an array using Stack.py diff --git a/Stacks problems/14 Delete middle element of a stack.py b/Stack & Queue/Stacks problems/14 Delete middle element of a stack.py similarity index 100% rename from Stacks problems/14 Delete middle element of a stack.py rename to Stack & Queue/Stacks problems/14 Delete middle element of a stack.py diff --git a/Stacks problems/2. Implement Stack and Queue using Deque.py b/Stack & Queue/Stacks problems/2. Implement Stack and Queue using Deque.py similarity index 100% rename from Stacks problems/2. Implement Stack and Queue using Deque.py rename to Stack & Queue/Stacks problems/2. Implement Stack and Queue using Deque.py diff --git a/Stacks problems/3. Implement Stack using Queues.py b/Stack & Queue/Stacks problems/3. Implement Stack using Queues.py similarity index 100% rename from Stacks problems/3. Implement Stack using Queues.py rename to Stack & Queue/Stacks problems/3. Implement Stack using Queues.py diff --git a/Stacks problems/4. Design a stack with operations on middle element.py b/Stack & Queue/Stacks problems/4. Design a stack with operations on middle element.py similarity index 100% rename from Stacks problems/4. Design a stack with operations on middle element.py rename to Stack & Queue/Stacks problems/4. Design a stack with operations on middle element.py diff --git a/Stacks problems/5. Design and Implement Special Stack Data Structure | Added Space Optimized Version.py b/Stack & Queue/Stacks problems/5. Design and Implement Special Stack Data Structure | Added Space Optimized Version.py similarity index 100% rename from Stacks problems/5. Design and Implement Special Stack Data Structure | Added Space Optimized Version.py rename to Stack & Queue/Stacks problems/5. Design and Implement Special Stack Data Structure | Added Space Optimized Version.py diff --git a/Stacks problems/6. Reverse a String using Stack.py b/Stack & Queue/Stacks problems/6. Reverse a String using Stack.py similarity index 100% rename from Stacks problems/6. Reverse a String using Stack.py rename to Stack & Queue/Stacks problems/6. Reverse a String using Stack.py diff --git a/Stacks problems/7. Parenthesis Checker.py b/Stack & Queue/Stacks problems/7. Parenthesis Checker.py similarity index 100% rename from Stacks problems/7. Parenthesis Checker.py rename to Stack & Queue/Stacks problems/7. Parenthesis Checker.py diff --git a/Stacks problems/8. Reverse a Stack using queue.py b/Stack & Queue/Stacks problems/8. Reverse a Stack using queue.py similarity index 100% rename from Stacks problems/8. Reverse a Stack using queue.py rename to Stack & Queue/Stacks problems/8. Reverse a Stack using queue.py diff --git a/Stacks problems/9. Stack using singly Linked_List.py b/Stack & Queue/Stacks problems/9. Stack using singly Linked_List.py similarity index 100% rename from Stacks problems/9. Stack using singly Linked_List.py rename to Stack & Queue/Stacks problems/9. Stack using singly Linked_List.py diff --git a/Stacks problems/Problems Practice.ipynb b/Stack & Queue/Stacks problems/Problems Practice.ipynb similarity index 100% rename from Stacks problems/Problems Practice.ipynb rename to Stack & Queue/Stacks problems/Problems Practice.ipynb diff --git a/Stacks problems/Stack.ipynb b/Stack & Queue/Stacks problems/Stack.ipynb similarity index 100% rename from Stacks problems/Stack.ipynb rename to Stack & Queue/Stacks problems/Stack.ipynb diff --git a/Stacks problems/Untitled.ipynb b/Stack & Queue/Stacks problems/Untitled.ipynb similarity index 100% rename from Stacks problems/Untitled.ipynb rename to Stack & Queue/Stacks problems/Untitled.ipynb diff --git a/Tree Data Structure.ipynb b/Tree/Tree Data Structure.ipynb similarity index 100% rename from Tree Data Structure.ipynb rename to Tree/Tree Data Structure.ipynb diff --git a/dp/Easy/Climbing_Stairs.py b/dp/Easy/Climbing_Stairs.py new file mode 100644 index 00000000..dd9b1431 --- /dev/null +++ b/dp/Easy/Climbing_Stairs.py @@ -0,0 +1,4 @@ +# Problem URL: https://leetcode.com/problems/climbing-stairs/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Easy/Cutted_Segments.py b/dp/Easy/Cutted_Segments.py new file mode 100644 index 00000000..97e589c3 --- /dev/null +++ b/dp/Easy/Cutted_Segments.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/cutted-segments1642/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Easy/Pairs_With_Specific_Difference.py b/dp/Easy/Pairs_With_Specific_Difference.py new file mode 100644 index 00000000..0941fb45 --- /dev/null +++ b/dp/Easy/Pairs_With_Specific_Difference.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/pairs-with-specific-difference1533/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Hard/Coin_Change.py b/dp/Hard/Coin_Change.py new file mode 100644 index 00000000..bd8bafae --- /dev/null +++ b/dp/Hard/Coin_Change.py @@ -0,0 +1,4 @@ +# Problem URL: https://leetcode.com/problems/coin-change/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Hard/Coin_Game_Winner.py b/dp/Hard/Coin_Game_Winner.py new file mode 100644 index 00000000..a3c80539 --- /dev/null +++ b/dp/Hard/Coin_Game_Winner.py @@ -0,0 +1,4 @@ +# Problem URL: https://www.geeksforgeeks.org/coin-game-winner-every-player-three-choices/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Hard/Count_Subsequences_Product_Less_K.py b/dp/Hard/Count_Subsequences_Product_Less_K.py new file mode 100644 index 00000000..4882d6f5 --- /dev/null +++ b/dp/Hard/Count_Subsequences_Product_Less_K.py @@ -0,0 +1,4 @@ +# Problem URL: https://www.geeksforgeeks.org/count-subsequences-product-less-k/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Hard/Edit_Distance.py b/dp/Hard/Edit_Distance.py new file mode 100644 index 00000000..657ff3ff --- /dev/null +++ b/dp/Hard/Edit_Distance.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/edit-distance3702/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Hard/Egg_Dropping_Puzzle.py b/dp/Hard/Egg_Dropping_Puzzle.py new file mode 100644 index 00000000..a4a542b8 --- /dev/null +++ b/dp/Hard/Egg_Dropping_Puzzle.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/egg-dropping-puzzle-1587115620/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Hard/Interleaved_Strings.py b/dp/Hard/Interleaved_Strings.py new file mode 100644 index 00000000..fe8131f5 --- /dev/null +++ b/dp/Hard/Interleaved_Strings.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/interleaved-strings/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Hard/Matrix_Chain_Multiplication.py b/dp/Hard/Matrix_Chain_Multiplication.py new file mode 100644 index 00000000..0cf63f42 --- /dev/null +++ b/dp/Hard/Matrix_Chain_Multiplication.py @@ -0,0 +1,4 @@ +# Problem URL: https://www.geeksforgeeks.org/matrix-chain-multiplication-dp-8/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Hard/Maximum_Profit_2.py b/dp/Hard/Maximum_Profit_2.py new file mode 100644 index 00000000..9df4f3af --- /dev/null +++ b/dp/Hard/Maximum_Profit_2.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/maximum-profit4657/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Hard/Maximum_Profit_By_Buying_and_Selling_At_Most_Twice.py b/dp/Hard/Maximum_Profit_By_Buying_and_Selling_At_Most_Twice.py new file mode 100644 index 00000000..811abb08 --- /dev/null +++ b/dp/Hard/Maximum_Profit_By_Buying_and_Selling_At_Most_Twice.py @@ -0,0 +1,4 @@ +# Problem URL: https://www.geeksforgeeks.org/maximum-profit-by-buying-and-selling-a-share-at-most-twice/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Hard/Optimal_Binary_Search_Tree.py b/dp/Hard/Optimal_Binary_Search_Tree.py new file mode 100644 index 00000000..dad83036 --- /dev/null +++ b/dp/Hard/Optimal_Binary_Search_Tree.py @@ -0,0 +1,4 @@ +# Problem URL: https://www.geeksforgeeks.org/optimal-binary-search-tree-dp-24/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Hard/Optimal_Strategy_for_A_Game.py b/dp/Hard/Optimal_Strategy_for_A_Game.py new file mode 100644 index 00000000..498cf43e --- /dev/null +++ b/dp/Hard/Optimal_Strategy_for_A_Game.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/optimal-strategy-for-a-game-1587115620/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Hard/Weighted_Job_Scheduling.py b/dp/Hard/Weighted_Job_Scheduling.py new file mode 100644 index 00000000..1beaacd2 --- /dev/null +++ b/dp/Hard/Weighted_Job_Scheduling.py @@ -0,0 +1,4 @@ +# Problem URL: https://www.geeksforgeeks.org/weighted-job-scheduling/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Assembly_Line_Scheduling.py b/dp/Medium/Assembly_Line_Scheduling.py new file mode 100644 index 00000000..53038313 --- /dev/null +++ b/dp/Medium/Assembly_Line_Scheduling.py @@ -0,0 +1,4 @@ +# Problem URL: https://www.geeksforgeeks.org/assembly-line-scheduling-dp-34/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/BBT_Counter.py b/dp/Medium/BBT_Counter.py new file mode 100644 index 00000000..c3bfd5e9 --- /dev/null +++ b/dp/Medium/BBT_Counter.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/bbt-counter4914/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Combination_Sum_IV.py b/dp/Medium/Combination_Sum_IV.py new file mode 100644 index 00000000..f9c7a300 --- /dev/null +++ b/dp/Medium/Combination_Sum_IV.py @@ -0,0 +1,4 @@ +# Problem URL: https://leetcode.com/problems/combination-sum-iv/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Count_Derangements.py b/dp/Medium/Count_Derangements.py new file mode 100644 index 00000000..c4459a06 --- /dev/null +++ b/dp/Medium/Count_Derangements.py @@ -0,0 +1,4 @@ +# Problem URL: https://www.geeksforgeeks.org/count-derangements-permutation-such-that-no-element-appears-in-its-original-position/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Count_Palindromic_Subsequences.py b/dp/Medium/Count_Palindromic_Subsequences.py new file mode 100644 index 00000000..18dd31ce --- /dev/null +++ b/dp/Medium/Count_Palindromic_Subsequences.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/count-palindromic-subsequences/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Decode_Ways.py b/dp/Medium/Decode_Ways.py new file mode 100644 index 00000000..eabdb448 --- /dev/null +++ b/dp/Medium/Decode_Ways.py @@ -0,0 +1,4 @@ +# Problem URL: https://leetcode.com/problems/decode-ways/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Gold_Mine_Problem.py b/dp/Medium/Gold_Mine_Problem.py new file mode 100644 index 00000000..7163742e --- /dev/null +++ b/dp/Medium/Gold_Mine_Problem.py @@ -0,0 +1,4 @@ +# Problem URL: https://www.geeksforgeeks.org/gold-mine-problem/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/House_Robber.py b/dp/Medium/House_Robber.py new file mode 100644 index 00000000..3a815f16 --- /dev/null +++ b/dp/Medium/House_Robber.py @@ -0,0 +1,4 @@ +# Problem URL: https://leetcode.com/problems/house-robber/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/House_Robber_II.py b/dp/Medium/House_Robber_II.py new file mode 100644 index 00000000..46832818 --- /dev/null +++ b/dp/Medium/House_Robber_II.py @@ -0,0 +1,4 @@ +# Problem URL: https://leetcode.com/problems/house-robber-ii/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Jump_Game.py b/dp/Medium/Jump_Game.py new file mode 100644 index 00000000..f8e71bb3 --- /dev/null +++ b/dp/Medium/Jump_Game.py @@ -0,0 +1,4 @@ +# Problem URL: https://leetcode.com/problems/jump-game/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Knapsack_0_1.py b/dp/Medium/Knapsack_0_1.py new file mode 100644 index 00000000..38478679 --- /dev/null +++ b/dp/Medium/Knapsack_0_1.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/0-1-knapsack-problem0945/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Knapsack_with_Duplicate_Items.py b/dp/Medium/Knapsack_with_Duplicate_Items.py new file mode 100644 index 00000000..39f8fe1a --- /dev/null +++ b/dp/Medium/Knapsack_with_Duplicate_Items.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/knapsack-with-duplicate-items4201/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Largest_Rectangular_Submatrix_Whose_Sum_0.py b/dp/Medium/Largest_Rectangular_Submatrix_Whose_Sum_0.py new file mode 100644 index 00000000..79bee379 --- /dev/null +++ b/dp/Medium/Largest_Rectangular_Submatrix_Whose_Sum_0.py @@ -0,0 +1,4 @@ +# Problem URL: https://www.geeksforgeeks.org/largest-rectangular-sub-matrix-whose-sum-0/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Largest_Square_In_Matrix.py b/dp/Medium/Largest_Square_In_Matrix.py new file mode 100644 index 00000000..91fc6243 --- /dev/null +++ b/dp/Medium/Largest_Square_In_Matrix.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/largest-square-formed-in-a-matrix0806/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Longest_Alternating_Subsequence.py b/dp/Medium/Longest_Alternating_Subsequence.py new file mode 100644 index 00000000..43fdb304 --- /dev/null +++ b/dp/Medium/Longest_Alternating_Subsequence.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/longest-alternating-subsequence5951/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Longest_Common_Subsequence.py b/dp/Medium/Longest_Common_Subsequence.py new file mode 100644 index 00000000..9795617e --- /dev/null +++ b/dp/Medium/Longest_Common_Subsequence.py @@ -0,0 +1,4 @@ +# Problem URL: https://leetcode.com/problems/longest-common-subsequence/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Longest_Common_Substring.py b/dp/Medium/Longest_Common_Substring.py new file mode 100644 index 00000000..c0e4f679 --- /dev/null +++ b/dp/Medium/Longest_Common_Substring.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/longest-common-substring1452/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Longest_Increasing_Subsequence.py b/dp/Medium/Longest_Increasing_Subsequence.py new file mode 100644 index 00000000..2b5d3564 --- /dev/null +++ b/dp/Medium/Longest_Increasing_Subsequence.py @@ -0,0 +1,4 @@ +# Problem URL: https://leetcode.com/problems/longest-increasing-subsequence/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Longest_Palindromic_Subsequence.py b/dp/Medium/Longest_Palindromic_Subsequence.py new file mode 100644 index 00000000..1624a5bc --- /dev/null +++ b/dp/Medium/Longest_Palindromic_Subsequence.py @@ -0,0 +1,4 @@ +# Problem URL: https://www.geeksforgeeks.org/longest-palindromic-subsequence-dp-12/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Longest_Repeating_Subsequence.py b/dp/Medium/Longest_Repeating_Subsequence.py new file mode 100644 index 00000000..b81b831f --- /dev/null +++ b/dp/Medium/Longest_Repeating_Subsequence.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/longest-repeating-subsequence2004/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Longest_Subsequence_Diff_One.py b/dp/Medium/Longest_Subsequence_Diff_One.py new file mode 100644 index 00000000..a31d6763 --- /dev/null +++ b/dp/Medium/Longest_Subsequence_Diff_One.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/longest-subsequence-such-that-difference-between-adjacents-is-one4724/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Max_Difference_of_Zeros_and_Ones.py b/dp/Medium/Max_Difference_of_Zeros_and_Ones.py new file mode 100644 index 00000000..3924664c --- /dev/null +++ b/dp/Medium/Max_Difference_of_Zeros_and_Ones.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/maximum-difference-of-zeros-and-ones-in-binary-string4111/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Max_Length_Chain.py b/dp/Medium/Max_Length_Chain.py new file mode 100644 index 00000000..534ec8e3 --- /dev/null +++ b/dp/Medium/Max_Length_Chain.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/max-length-chain/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Max_Subsequence_Sum_No_Three_Consecutive.py b/dp/Medium/Max_Subsequence_Sum_No_Three_Consecutive.py new file mode 100644 index 00000000..57107922 --- /dev/null +++ b/dp/Medium/Max_Subsequence_Sum_No_Three_Consecutive.py @@ -0,0 +1,4 @@ +# Problem URL: https://www.geeksforgeeks.org/maximum-subsequence-sum-such-that-no-three-are-consecutive/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Max_Sum_Increasing_Subsequence.py b/dp/Medium/Max_Sum_Increasing_Subsequence.py new file mode 100644 index 00000000..7e0418b5 --- /dev/null +++ b/dp/Medium/Max_Sum_Increasing_Subsequence.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/maximum-sum-increasing-subsequence4749/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Maximum_Length_of_Pair_Chain.py b/dp/Medium/Maximum_Length_of_Pair_Chain.py new file mode 100644 index 00000000..20dbc7e8 --- /dev/null +++ b/dp/Medium/Maximum_Length_of_Pair_Chain.py @@ -0,0 +1,4 @@ +# Problem URL: https://leetcode.com/problems/maximum-length-of-pair-chain/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Minimum_Jumps.py b/dp/Medium/Minimum_Jumps.py new file mode 100644 index 00000000..da81878f --- /dev/null +++ b/dp/Medium/Minimum_Jumps.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/minimum-number-of-jumps-1587115620/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Minimum_Removals_Array.py b/dp/Medium/Minimum_Removals_Array.py new file mode 100644 index 00000000..8e6e0854 --- /dev/null +++ b/dp/Medium/Minimum_Removals_Array.py @@ -0,0 +1,4 @@ +# Problem URL: http://geeksforgeeks.org/minimum-removals-array-make-max-min-k/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Mobile_Numeric_Keypad.py b/dp/Medium/Mobile_Numeric_Keypad.py new file mode 100644 index 00000000..2e3b0f4b --- /dev/null +++ b/dp/Medium/Mobile_Numeric_Keypad.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/mobile-numeric-keypad5456/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/NCR.py b/dp/Medium/NCR.py new file mode 100644 index 00000000..93ec10e1 --- /dev/null +++ b/dp/Medium/NCR.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/ncr1019/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Nth_Catalan_Number.py b/dp/Medium/Nth_Catalan_Number.py new file mode 100644 index 00000000..c081ac79 --- /dev/null +++ b/dp/Medium/Nth_Catalan_Number.py @@ -0,0 +1,4 @@ +# Problem URL: https://www.geeksforgeeks.org/program-nth-catalan-number/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Path_In_Matrix.py b/dp/Medium/Path_In_Matrix.py new file mode 100644 index 00000000..b86d90c5 --- /dev/null +++ b/dp/Medium/Path_In_Matrix.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/path-in-matrix3805/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Permutation_Coefficient.py b/dp/Medium/Permutation_Coefficient.py new file mode 100644 index 00000000..1cb9efe2 --- /dev/null +++ b/dp/Medium/Permutation_Coefficient.py @@ -0,0 +1,4 @@ +# Problem URL: https://www.geeksforgeeks.org/permutation-coefficient/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Reach_A_Given_Score.py b/dp/Medium/Reach_A_Given_Score.py new file mode 100644 index 00000000..f69b7dd9 --- /dev/null +++ b/dp/Medium/Reach_A_Given_Score.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/reach-a-given-score-1587115621/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Subset_Sum_Problem.py b/dp/Medium/Subset_Sum_Problem.py new file mode 100644 index 00000000..155272e4 --- /dev/null +++ b/dp/Medium/Subset_Sum_Problem.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/subset-sum-problem2014/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Unique_Paths.py b/dp/Medium/Unique_Paths.py new file mode 100644 index 00000000..35ec39c9 --- /dev/null +++ b/dp/Medium/Unique_Paths.py @@ -0,0 +1,4 @@ +# Problem URL: https://leetcode.com/problems/unique-paths/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Word_Break.py b/dp/Medium/Word_Break.py new file mode 100644 index 00000000..8260101c --- /dev/null +++ b/dp/Medium/Word_Break.py @@ -0,0 +1,4 @@ +# Problem URL: https://leetcode.com/problems/word-break/ +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/dp/Medium/Word_Wrap.py b/dp/Medium/Word_Wrap.py new file mode 100644 index 00000000..7a36f70d --- /dev/null +++ b/dp/Medium/Word_Wrap.py @@ -0,0 +1,4 @@ +# Problem URL: https://practice.geeksforgeeks.org/problems/word-wrap1646/1 +class Solution: + def solve(self, *args, **kwargs): + pass diff --git a/graph/.ipynb_checkpoints/Graph-checkpoint.ipynb b/graph/.ipynb_checkpoints/Graph-checkpoint.ipynb new file mode 100644 index 00000000..e131baf1 --- /dev/null +++ b/graph/.ipynb_checkpoints/Graph-checkpoint.ipynb @@ -0,0 +1,33 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "ed34e458-e82e-42f9-8e19-cd52d6952048", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "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.11.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/graph/Graph.ipynb b/graph/Graph.ipynb new file mode 100644 index 00000000..76c2d3eb --- /dev/null +++ b/graph/Graph.ipynb @@ -0,0 +1,88 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "d129f9c0", + "metadata": {}, + "source": [ + "### Graph Data Structure: Simplified Notes\n", + "\n", + "**Definition:**\n", + "- A **graph** is a non-linear data structure with **vertices** (nodes) and **edges** (lines or arcs).\n", + "- **Vertices**: Fundamental units, can be labeled or unlabeled.\n", + "- **Edges**: Connect two nodes, can be directed or undirected, and labeled or unlabeled.\n", + "- Denoted as \\( G(V, E) \\) where \\( V \\) is the set of vertices and \\( E \\) is the set of edges.\n" + ] + }, + { + "cell_type": "markdown", + "id": "321141ca", + "metadata": {}, + "source": [ + "\"Graph" + ] + }, + { + "cell_type": "markdown", + "id": "0a34950a", + "metadata": {}, + "source": [ + "\n", + "**Components:**\n", + "1. **Vertices (Nodes)**: Individual entities in the graph.\n", + "2. **Edges (Arcs)**: Connections between nodes.\n", + "\n", + "**Basic Operations:**\n", + "1. **Insertion**: Add nodes or edges.\n", + "2. **Deletion**: Remove nodes or edges.\n", + "3. **Searching**: Locate entities in the graph.\n", + "4. **Traversal**: Visit all nodes.\n", + "\n", + "**Advanced Operations:**\n", + "1. **Shortest Paths**: Find shortest paths between nodes.\n", + "2. **Minimum Spanning Tree**: Find minimum weight edges to connect all nodes in a weighted, undirected graph.\n", + "\n", + "**Applications:**\n", + "- **Social Networks**: Nodes represent users, edges represent connections (friends, followers).\n", + "- **Neural Networks**: Nodes are neurons, edges are synapses.\n", + "- **Compilers**: Used for type inference, data flow analysis, etc.\n", + "- **Robot Planning**: Nodes represent states, edges represent transitions.\n", + "- **GPS Navigation**: Find shortest routes and locations.\n", + "- **Network Design**: Optimize connections in networks (e.g., minimizing wire length).\n", + "- **Sports Analytics**: Represent player interactions and team dynamics.\n", + "- **Computer Networks**: Represent connections between routers and switches.\n", + "- **Transportation**: Represent connections between locations (e.g., roads, airports).\n", + "- **Project Management**: Represent task dependencies (topological sorting)." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "554632fc", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "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.11.7" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Algorithms.ipynb b/introduction/Algorithms.ipynb similarity index 100% rename from Algorithms.ipynb rename to introduction/Algorithms.ipynb diff --git a/Built-In Data Structure.ipynb b/introduction/Built-In Data Structure.ipynb similarity index 100% rename from Built-In Data Structure.ipynb rename to introduction/Built-In Data Structure.ipynb diff --git a/Data Structure.ipynb b/introduction/Data Structure.ipynb similarity index 100% rename from Data Structure.ipynb rename to introduction/Data Structure.ipynb diff --git a/Problem Solving.ipynb b/introduction/Problem Solving.ipynb similarity index 100% rename from Problem Solving.ipynb rename to introduction/Problem Solving.ipynb diff --git a/introduction ineuron.ipynb b/introduction/introduction ineuron.ipynb similarity index 100% rename from introduction ineuron.ipynb rename to introduction/introduction ineuron.ipynb diff --git a/markdown.txt b/markdown.txt deleted file mode 100644 index 210cc9a2..00000000 --- a/markdown.txt +++ /dev/null @@ -1,3 +0,0 @@ -
-
missing
-
\ No newline at end of file diff --git a/math/3. Integer to English Words.py b/math/3. Integer to English Words.py index e69de29b..175c5035 100644 --- a/math/3. Integer to English Words.py +++ b/math/3. Integer to English Words.py @@ -0,0 +1,50 @@ +# https://leetcode.com/problems/integer-to-english-words/description/ + +class Solution: + def numberToWords(self, num: int) -> str: + dict = {0: "Zero", + 1: "One", + 2: "Two", + 3: "Three", + 4: "Four", + 5: "Five", + 6: "Six", + 7: "Seven", + 8: "Eight", + 9: "Nine", + 10: "Ten", + 11: "Eleven", + 12: "Twelve", + 13: "Thirteen", + 14: "Fourteen", + 15: "Fifteen", + 16: "Sixteen", + 17: "Seventeen", + 18: "Eighteen", + 19: "Nineteen", + 20: "Twenty", + 30: "Thirty", + 40: "Forty", + 50: "Fifty", + 60: "Sixty", + 70: "Seventy", + 80: "Eighty", + 90: "Ninety", + 100: "Hundred", + 1000: "Thousand", + 1000000: "Million", + 1000000000: "Billion" + } + english='' + while num: + mod=num%10 + first=num//10 + temp=num-mod + english=dict[first] + num/=10 + return english + + +if __name__ == '__main__': + a=Solution().numberToWords(300) + print(a) \ No newline at end of file