일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- Python
- 315. Count of Smaller Numbers After Self
- Protocol
- 컴퓨터의 구조
- 운영체제
- Convert Sorted List to Binary Search Tree
- t1
- data science
- iterator
- Python Implementation
- 109. Convert Sorted List to Binary Search Tree
- 43. Multiply Strings
- Decorator
- Regular Expression
- Generator
- LeetCode
- Substring with Concatenation of All Words
- 30. Substring with Concatenation of All Words
- Python Code
- concurrency
- Class
- 715. Range Module
- 파이썬
- 밴픽
- kaggle
- DWG
- attribute
- 시바견
- 프로그래머스
- shiba
Archives
- Today
- Total
Scribbling
LeetCode: 138. Copy List with Random Pointer 본문
Computer Science/Coding Test
LeetCode: 138. Copy List with Random Pointer
focalpoint 2021. 11. 16. 21:44O(N) Time, O(N) Memory
"""
# Definition for a Node.
class Node:
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
self.val = int(x)
self.next = next
self.random = random
"""
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if not head:
return
cur, dic = head, {}
while cur:
dic[cur] = Node(x=cur.val)
cur = cur.next
cur = head
while cur:
if cur.next:
dic[cur].next = dic[cur.next]
if cur.random:
dic[cur].random = dic[cur.random]
cur = cur.next
return dic[head]
O(N) Time, O(1) Memory
"""
# Definition for a Node.
class Node:
def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None):
self.val = int(x)
self.next = next
self.random = random
"""
class Solution:
def copyRandomList(self, head: 'Node') -> 'Node':
if not head:
return
cur = head
while cur:
nxt = cur.next
new_node = Node(x=cur.val)
cur.next = new_node
new_node.next = nxt
cur = nxt
cur = head
while cur:
if cur.random:
cur.next.random = cur.random.next
cur = cur.next.next
cur = head.next
while cur.next:
nxt = cur.next.next
cur.next = nxt
cur = nxt
return head.next
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 140. Word Break II (0) | 2021.11.22 |
---|---|
LeetCode: 139. Word Break (0) | 2021.11.19 |
LeetCode: 295. Find Median from Data Stream (0) | 2021.11.16 |
LeetCode: 347. Top K Frequent Elements (0) | 2021.11.15 |
LeetCode: 354. Russian Doll Envelopes (0) | 2021.11.15 |