Copy List with Random Pointer | Leetcode 138

preview_player
Показать описание

1) 0:00 Explaining the problem out loud
2) 1:10 Algorithm walkthrough
3) 8:15 Coding it up
4) 14:00 TimeComplexity


Рекомендации по теме
Комментарии
Автор

Nice trick and I was there solving it O(N^2) by scanning through the whole list for each node to classify random.

mayankchauhan
Автор

Hi,
Thanks for this solution. Could you please let me know if below approach is also fine:
1. Created a dummy node
2. Iterated over the original list and created new nodes, with the original list's node val and linked new list nodes together.
3. Also, while scanning in the original list, I have maintained two maps - one which takes care of the old to new node mapping and the second one which takes care of the old node to its random node link.
4. Then, I loop over the oldNewMap and create links for the random pointer in the new node.
5. Finally, I return dummy.next

abhishekjain
welcome to shbcf.ru