Copy List with Random Pointer - Linked List - Leetcode 138

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


0:00 - Read the problem
5:07 - Coding Explanation

leetcode 138

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

this time I figured out by myself that I would need 2 passes, 1 for the nodes creation and a second one for the linking, also I figured a hashmap would be the best way of accessing the copies. However, your solution is by far more simple and elegant. Thanks so much for inspiring us to keep practicing!

aaen
Автор

NeetCode gives the best Leetcode explanations on YouTube, imo

MrPaulzeng
Автор

You were right, it's much easier to understand the solution by looking at the code than to explain it. Well done!

tnmyk_
Автор

Just wanted to say you are awesome, and thank you for your time and effort that you put into your videos! Much appreciated 🙏

bernardpark
Автор

I solved the problem with some 10x more complicated logic using the id function to uniquely identify each node by it's memory address, and from there on making a hashmap that maps the memory address to the node, all because I had no idea you could assign an object as hashmap key... feels really nice to learn that but I am definitely feeling pretty stupid right now LOL

fredtrentini
Автор

I struggled with 3-4 Linked List questions from NeetCode list and watched and learnt from your videos. And finally was able to come up with the solution similar to yours without watching the video. Thank you. Your way of explaining algorithm is effortless. Please make a video on how to explain your thoughts about a question in an interview?

sumitghewade
Автор

Your videos are great you have no idea how much you've helped me! For an alternative solution I also used a hashmap from nodes in the original list to nodes in the new list, but I was able to do this all in one pass! You can simply create the connections and create the new nodes as necessary, adding them to the hashmap. Then, if at any point when adding a connection for "next" or "random" you find that you've already created a node for the corresponding node in the original list then you can go ahead and use that pointer. Hope this helps someone!

Demo-man
Автор

This can also be done with no extra space (besides what's needed for making the new list). It creates the nodes of the new list directly woven in-between the nodes of the original. Discovered I could do it in this way after using the Hint button on LeetCode.

def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
if head is None:
return None

ptr = head
while ptr:
new = Node(ptr.val, ptr.next)
ptr.next = new
ptr = ptr.next.next

ptr = head
while ptr:
copy = ptr.next
copy.random = ptr.random.next if ptr.random else None
ptr = copy.next

ptr = head.next
while ptr:
ptr.next = ptr.next.next if ptr.next else None
ptr = ptr.next

return head.next

danielrodrigues
Автор

You have to know that exists second solution with O(1) space. It is possible if you change input linked list with appending clonned values between existed nodes in linked list.

mrkandreev
Автор

I came up with very complicated solution and was amazed to see ur simple solution.Thanks a lot for your time and efforts

ananya___
Автор

this can be done in O(1) space by interleaving the old and new nodes while creating.
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
if not head: return head
ptr = head
while ptr:
newnode = Node(ptr.val, ptr.next)
ptr.next = newnode
ptr = newnode.next

ptr = head
while ptr:
ptr.next.random = ptr.random.next if ptr.random else None
ptr = ptr.next.next

ptr = head.next
while ptr:
ptr.next = ptr.next.next if ptr.next else None
ptr = ptr.next

return head.next

sayanghosh
Автор

I was complicating this by linking the new nodes except the random and decided to link the random ptr in the next pass, but your solution eases this out, thank you so much for posting this!

rahulsbhatt
Автор

It can be done via just _one_ traversal in recursive DFS manner

class Solution:
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
cloned = {}

def dfs(node: 'Optional[Node]') -> 'Optional[Node]':
if node is None:
return None
if node in cloned:
return cloned[node]

clone = Node(node.val)
cloned[node] = clone
clone . next = dfs(node.next)
clone.random = dfs(node.random)

return clone

return dfs(head)

romangirin
Автор

Cool approach. I added an index property to hash the nodes in Javascript, since attempting to hash with the node itself will lead to the key being "[object Object]" rather than the memory address as I imagine it does in python.

eugenevedensky
Автор

There is not a single Neetcode video that doesn't help me understand a problem. Much love and appreciation!

htoomyatnaing
Автор

Please do more problems. I really love to watch your explanations after I give the problem a try.

kushagrabansal
Автор

Thank you Fords!! I'm not well versed in Python, but when I looked at your Java code it's very clear.

alexshay
Автор

I think showing this with the memory addresses would make it more clear. Basically we made a dictionary where the key is the orignal nodes memory address or in python a pointer to the nodes memory address. The value is the coppied nodes memory address/ a pointer to it.

souljarohill
Автор

You can use O(1) space for this problem. On pass one, build next links for the new list and establish a two way relationship with the matching node from the second list by using the unused random pointer of list2 to point to list 1. You can also break the next pointer on list1 and use it to point to the list2 node. Then on pass two use those pointers to get to the random node and build the random pointers.

Matt-qrxw
Автор

I really like this problem! Thanks for making these videos to help us all see the algorithms and thought process clearly!

GoziePO