|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "f6737794-c5b1-44dc-83e6-149a1a6987fb", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# **7.2 Reverse a Single Sublist**\n", |
| 9 | + "---\n", |
| 10 | + "- Reversing a sublist within a list \n", |
| 11 | + "- Input = singly linked list `L` and two integers `s` and `f`\n", |
| 12 | + "- Reverse nodes from `s` to `f` INCLUSIVE\n", |
| 13 | + "- Succesor fields also have to be updated \n", |
| 14 | + "- Iterate through list until you hit `s` and predecessor \n", |
| 15 | + "- `s` node begins reversal process and we count until we reach `f`\n", |
| 16 | + "- Relink reversed sublist with rest of sublist \n" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": 1, |
| 22 | + "id": "780e7092-9cf1-4789-9b81-c8885169e3f0", |
| 23 | + "metadata": {}, |
| 24 | + "outputs": [], |
| 25 | + "source": [ |
| 26 | + "from typing import List\n", |
| 27 | + "from typing import Optional" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "markdown", |
| 32 | + "id": "bcec5090-22a3-4e56-b068-2227e12f4b0a", |
| 33 | + "metadata": {}, |
| 34 | + "source": [ |
| 35 | + "---\n", |
| 36 | + "## Reverse Sublist" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "raw", |
| 41 | + "id": "7ec845cd-10ae-4d74-be3a-3b3aba23d346", |
| 42 | + "metadata": { |
| 43 | + "tags": [] |
| 44 | + }, |
| 45 | + "source": [ |
| 46 | + " def reverse_sub(self, start: int, finish: int) -> Optional[ListNode]:\n", |
| 47 | + " \n", |
| 48 | + " sentinel = sublist_head = ListNode(0,L)\n", |
| 49 | + " \n", |
| 50 | + " for _ in range(1,start):\n", |
| 51 | + " sublist_head = sublist_head.next\n", |
| 52 | + " \n", |
| 53 | + " # Revese\n", |
| 54 | + " sublist_itr = sublist_head.next\n", |
| 55 | + " for _ in range(finish - start):\n", |
| 56 | + " temp = sublist_itr.next\n", |
| 57 | + " sublist_itr, temp.next = sublist_head.next = (temp.next, sublist_head.next, temp)\n", |
| 58 | + " \n", |
| 59 | + " return sentinel.next \n", |
| 60 | + " " |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": 4, |
| 66 | + "id": "01ccfdba-950e-4eee-ad34-19f72e3814a1", |
| 67 | + "metadata": {}, |
| 68 | + "outputs": [ |
| 69 | + { |
| 70 | + "name": "stdout", |
| 71 | + "output_type": "stream", |
| 72 | + "text": [ |
| 73 | + "10 20 30 40 50 60 70 \n", |
| 74 | + "10 20 60 50 40 30 70 \n" |
| 75 | + ] |
| 76 | + } |
| 77 | + ], |
| 78 | + "source": [ |
| 79 | + "class Node:\n", |
| 80 | + " def __init__(self, data):\n", |
| 81 | + " self.data = data\n", |
| 82 | + " self.next = None\n", |
| 83 | + " \n", |
| 84 | + "# goes with reverse subllist function\n", |
| 85 | + "def reverse(head):\n", |
| 86 | + " prev = None \n", |
| 87 | + " curr = head\n", |
| 88 | + " while (curr):\n", |
| 89 | + " next = curr.next\n", |
| 90 | + " curr.next = prev\n", |
| 91 | + " prev = curr\n", |
| 92 | + " curr = next\n", |
| 93 | + " return prev\n", |
| 94 | + "\n", |
| 95 | + "def reverseBetween(head, m, n):\n", |
| 96 | + " if (m == n):\n", |
| 97 | + " return head \n", |
| 98 | + " revs = None\n", |
| 99 | + " revs_prev = None\n", |
| 100 | + " revend = None\n", |
| 101 | + " revend_next = None\n", |
| 102 | + " i = 1\n", |
| 103 | + " curr = head\n", |
| 104 | + " while (curr and i <= n):\n", |
| 105 | + " if (i < m):\n", |
| 106 | + " revs_prev = curr\n", |
| 107 | + " if (i == m):\n", |
| 108 | + " revs = curr\n", |
| 109 | + " if (i == n):\n", |
| 110 | + " revend = curr\n", |
| 111 | + " revend_next = curr.next\n", |
| 112 | + " curr = curr.next\n", |
| 113 | + " i += 1\n", |
| 114 | + " revend.next = None\n", |
| 115 | + " revend = reverse(revs)\n", |
| 116 | + " if (revs_prev):\n", |
| 117 | + " revs_prev.next = revend\n", |
| 118 | + " else:\n", |
| 119 | + " head = revend\n", |
| 120 | + " revs.next = revend_next\n", |
| 121 | + " return head\n", |
| 122 | + " \n", |
| 123 | + "def prints(head):\n", |
| 124 | + " while (head != None):\n", |
| 125 | + " print(head.data, end = ' ')\n", |
| 126 | + " head = head.next\n", |
| 127 | + " print()\n", |
| 128 | + " \n", |
| 129 | + "def push(head_ref, new_data):\n", |
| 130 | + " \n", |
| 131 | + " new_node = Node(new_data)\n", |
| 132 | + " new_node.data = new_data\n", |
| 133 | + " new_node.next = (head_ref)\n", |
| 134 | + " (head_ref) = new_node\n", |
| 135 | + " return head_ref\n", |
| 136 | + " \n", |
| 137 | + " \n", |
| 138 | + "if __name__=='__main__':\n", |
| 139 | + " \n", |
| 140 | + " head = None\n", |
| 141 | + " head = push(head, 70)\n", |
| 142 | + " head = push(head, 60)\n", |
| 143 | + " head = push(head, 50)\n", |
| 144 | + " head = push(head, 40)\n", |
| 145 | + " head = push(head, 30)\n", |
| 146 | + " head = push(head, 20)\n", |
| 147 | + " head = push(head, 10)\n", |
| 148 | + " prints(head)\n", |
| 149 | + " \n", |
| 150 | + " reverseBetween(head, 3, 6)\n", |
| 151 | + " prints(head)" |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "markdown", |
| 156 | + "id": "518235a2-eea3-467b-92eb-e9e3e420fc31", |
| 157 | + "metadata": {}, |
| 158 | + "source": [ |
| 159 | + "##### Time Complexity: `O(f)`\n", |
| 160 | + "- `f` = end of reversal range " |
| 161 | + ] |
| 162 | + }, |
| 163 | + { |
| 164 | + "cell_type": "markdown", |
| 165 | + "id": "69520395-ba3d-4d90-99cd-37f90b59c4d4", |
| 166 | + "metadata": {}, |
| 167 | + "source": [ |
| 168 | + "---\n", |
| 169 | + "# Variant\n", |
| 170 | + "#### Reverse a Singly Linked List in Constant Space" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "code", |
| 175 | + "execution_count": 5, |
| 176 | + "id": "096fd3d5-c899-4c4a-95d0-8d348264e349", |
| 177 | + "metadata": {}, |
| 178 | + "outputs": [], |
| 179 | + "source": [ |
| 180 | + "class ListNode:\n", |
| 181 | + " def __init__(self, data=None, next=None):\n", |
| 182 | + " self.data = data\n", |
| 183 | + " self.next = next " |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "code", |
| 188 | + "execution_count": 6, |
| 189 | + "id": "464e4a49-b810-47ef-a6de-ab1c94c77b42", |
| 190 | + "metadata": { |
| 191 | + "tags": [] |
| 192 | + }, |
| 193 | + "outputs": [], |
| 194 | + "source": [ |
| 195 | + "class LinkedList:\n", |
| 196 | + " def __init__(self):\n", |
| 197 | + " self.head = None\n", |
| 198 | + " self.last_node = None\n", |
| 199 | + " \n", |
| 200 | + " def insert_at_beg(self,data):\n", |
| 201 | + " # inserting at beginning -> next element is the current head \n", |
| 202 | + " node = ListNode(data,self.head)\n", |
| 203 | + " self.head = node \n", |
| 204 | + " \n", |
| 205 | + " def reverse(self):\n", |
| 206 | + " prev = None\n", |
| 207 | + " current = self.head\n", |
| 208 | + " while(current is not None):\n", |
| 209 | + " next = current.next\n", |
| 210 | + " current.next = prev\n", |
| 211 | + " prev = current \n", |
| 212 | + " current = next\n", |
| 213 | + " self.head = prev \n", |
| 214 | + " \n", |
| 215 | + " def print(self):\n", |
| 216 | + " if self.head is None:\n", |
| 217 | + " print(\"Linked List is empty\")\n", |
| 218 | + " return\n", |
| 219 | + " itr = self.head\n", |
| 220 | + " llstr = ''\n", |
| 221 | + " while itr:\n", |
| 222 | + " llstr += str(itr.data) + '-->'\n", |
| 223 | + " itr = itr.next\n", |
| 224 | + " print(llstr)\n", |
| 225 | + " " |
| 226 | + ] |
| 227 | + }, |
| 228 | + { |
| 229 | + "cell_type": "code", |
| 230 | + "execution_count": 7, |
| 231 | + "id": "635234a5-66d8-4480-a374-e8ec2c5f645d", |
| 232 | + "metadata": {}, |
| 233 | + "outputs": [ |
| 234 | + { |
| 235 | + "name": "stdout", |
| 236 | + "output_type": "stream", |
| 237 | + "text": [ |
| 238 | + "60-->50-->40-->30-->20-->10-->\n", |
| 239 | + "10-->20-->30-->40-->50-->60-->\n" |
| 240 | + ] |
| 241 | + } |
| 242 | + ], |
| 243 | + "source": [ |
| 244 | + "if __name__ == '__main__':\n", |
| 245 | + " ll = LinkedList()\n", |
| 246 | + " ll.insert_at_beg(10)\n", |
| 247 | + " ll.insert_at_beg(20)\n", |
| 248 | + " ll.insert_at_beg(30)\n", |
| 249 | + " ll.insert_at_beg(40)\n", |
| 250 | + " ll.insert_at_beg(50)\n", |
| 251 | + " ll.insert_at_beg(60)\n", |
| 252 | + " ll.print()\n", |
| 253 | + " ll.reverse()\n", |
| 254 | + " ll.print()" |
| 255 | + ] |
| 256 | + }, |
| 257 | + { |
| 258 | + "cell_type": "markdown", |
| 259 | + "id": "667a798a-e1c2-43cb-843b-ebe7d442ea48", |
| 260 | + "metadata": {}, |
| 261 | + "source": [ |
| 262 | + "---\n", |
| 263 | + "# Variant\n" |
| 264 | + ] |
| 265 | + }, |
| 266 | + { |
| 267 | + "cell_type": "code", |
| 268 | + "execution_count": null, |
| 269 | + "id": "ed015ff1-09ae-4bc9-b2de-417a36c6614a", |
| 270 | + "metadata": {}, |
| 271 | + "outputs": [], |
| 272 | + "source": [] |
| 273 | + } |
| 274 | + ], |
| 275 | + "metadata": { |
| 276 | + "kernelspec": { |
| 277 | + "display_name": "Python 3", |
| 278 | + "language": "python", |
| 279 | + "name": "python3" |
| 280 | + }, |
| 281 | + "language_info": { |
| 282 | + "codemirror_mode": { |
| 283 | + "name": "ipython", |
| 284 | + "version": 3 |
| 285 | + }, |
| 286 | + "file_extension": ".py", |
| 287 | + "mimetype": "text/x-python", |
| 288 | + "name": "python", |
| 289 | + "nbconvert_exporter": "python", |
| 290 | + "pygments_lexer": "ipython3", |
| 291 | + "version": "3.8.8" |
| 292 | + } |
| 293 | + }, |
| 294 | + "nbformat": 4, |
| 295 | + "nbformat_minor": 5 |
| 296 | +} |
0 commit comments