Skip to content

Latest commit

 

History

History
52 lines (31 loc) · 2.13 KB

File metadata and controls

52 lines (31 loc) · 2.13 KB

Linked List Insertions

Write a function called zip_lists

  • Arguments: 2 linked lists
  • Return: New Linked List, zipped as noted below
  • Zip the two linked lists together into one so that the nodes alternate between the two lists and return a reference to the zipped list.
  • Try and keep additional space down to O(1)
  • This will use code from Linked List Insertions

Whiteboard Process

Code Challenge 8: figjam image, link below

Figjam Link for Code Challenge 8-- to navigate the image embedded above

Approach & Efficiency

In my code, I used one while loop and then an if conditional for each of the linked lists, then had each if conditional move across the nodes of both lists. Since my code wanted me to use the first list argument first in the zipping process, I put that one first in the while loop and list2 second. I suppose you could add more conditions.

Time: O(n+m)

  • because you need to traverse both of the linked lists, even though I have it in a nested while loop

Space: O(n+m)

  • this accounts for the space needed to store the nodes of the new linked list, which will have a number of nodes equal to the sum of the nodes in the two input lists

Solution

Terminal command: python3 -m linked_list_zip.linked_list_zip

Test code in main module: returns list3:  A -> B -> C -> Null A -> B -> C -> Null list4:  1 -> 2 -> 3 -> Null 1 -> 2 -> 3 -> Null zip_lists: A -> 1 -> B -> 2 -> C -> 3 -> Null

Terminal command : python3 -m pytest linked_list_zip/

Terminal of tests passing: collected 6 items, 6 passed in pytest

Code Links

Zip