Here, we need to return a list with smaller linked lists divided equally. Note, we do not need to return a list with lists containing the list nodes. We only need to return a list with head
nodes of the partitioned smaller lists.
Few things off my mind:
-
Since we need to have smaller linked lists, we need to iterate the larger list, iterate over required number of nodes, and mark the last node of this “partition” to
node.next = None
. This is what breaks the links between partition lists. -
We can calculate the window size required, so we know when to break a partition. Also important, that they have mentioned that the paritions’ size can only differ by one and the larger lists must be at the beginning of the result list.
-
By calculating the extra nodes by
E = list_size % k
. We can keep a counter of this and reduce by one for the firstE
nodes.
Python3
Big O Analysis
-
Runtime
The runtime complexity here is where N is the number of nodes in the linked list.
-
Memory
The memory usage is since we use the
collections.defaultdict
object to store multiple and modulos.
— A