This reverses the linked list order. >>> linked_list = LinkedList() >>> linked_list.insert_tail("first") >>> linked_list.insert_tail("second") >>> linked_list.insert_tail("third") >>> linked_list first -> second -> third >>> linked_lis
(self)
| 335 | return self.head is None |
| 336 | |
| 337 | def reverse(self) -> None: |
| 338 | """ |
| 339 | This reverses the linked list order. |
| 340 | >>> linked_list = LinkedList() |
| 341 | >>> linked_list.insert_tail("first") |
| 342 | >>> linked_list.insert_tail("second") |
| 343 | >>> linked_list.insert_tail("third") |
| 344 | >>> linked_list |
| 345 | first -> second -> third |
| 346 | >>> linked_list.reverse() |
| 347 | >>> linked_list |
| 348 | third -> second -> first |
| 349 | """ |
| 350 | prev = None |
| 351 | current = self.head |
| 352 | |
| 353 | while current: |
| 354 | # Store the current node's next node. |
| 355 | next_node = current.next_node |
| 356 | # Make the current node's next_node point backwards |
| 357 | current.next_node = prev |
| 358 | # Make the previous node be the current node |
| 359 | prev = current |
| 360 | # Make the current node the next_node node (to progress iteration) |
| 361 | current = next_node |
| 362 | # Return prev in order to put the head at the end |
| 363 | self.head = prev |
| 364 | |
| 365 | |
| 366 | def test_singly_linked_list() -> None: |
no outgoing calls