파비의 매일매일 공부기록

2023.09.05 Today's Challenge 본문

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
반응형

'Problem Solving > LeetCode' 카테고리의 다른 글

2023.09.07 Today's Challenge  (0) 2023.09.07
2023.09.06 Today's Challenge  (0) 2023.09.06
2023.09.04 Today's Challenge  (0) 2023.09.04
2023.09.03 Today's Challenge  (0) 2023.09.03
2023.09.02 Today's Challenge  (0) 2023.09.02
Comments