Problem Solving/LeetCode
2023.09.05 Today's Challenge
fabichoi
2023. 9. 5. 23:45
https://leetcode.com/problems/copy-list-with-random-pointer/
Copy List with Random Pointer - LeetCode
Can you solve this real interview question? Copy List with Random Pointer - A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list, or null. Construct a deep copy [https://en.
leetcode.com
링크드 리스트 문제
class Solution:
def __init__(self):
self.visited = {}
def copyRandomList(self, head: 'Optional[Node]') -> 'Optional[Node]':
if not head:
return None
if head in self.visited:
return self.visited[head]
newNode = Node(head.val)
self.visited[head] = newNode
newNode.next = self.copyRandomList(head.next)
newNode.random = self.copyRandomList(head.random)
return newNode
반응형