Intuition
The problem is very similar to removing a node from the linked list. But instead of one nodes, now there are multiple nodes that need deletion.
So we convert the nums
array into a set for constant time look-up. While iterating over the list if we come across any node that is present in this set, we set next
property of that node’s parent (previous node) to next.next
. This means we skip the node in the middle (to be deleted) and straight jump to the next node.
This is fundamental to how deletions in linked lists work.
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is since each node in the linked list is getting processed only once.
-
Memory
The memory usage is since we use a
set
for constant time look-up.
— A