Intuition
The problem is straight forward in that we need to insert another list in between list 1. We are given a
and b
- so we need to insert list2
in place of to nodes.
If we have the node just before the nodes, we can just startNode.next = list2
.
Similarly, if we have the corresponding node, we can just list2.next = endNode
- given we iterate list2
till the end.
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is where N is the number of nodes in
list1
orlist2
which ever is longest. -
Memory
The memory usage is since we are not using any extra data structure.
— A