Copy List With Random Pointer
Detailed guide and Python implementation for the 'Copy List With Random Pointer' problem.
1. 学ぶ
The 'Copy List With Random Pointer' problem is a key challenge in the Linked List section.
This implementation focuses on easy-level logic in Python.
We prioritize technical accuracy and code readability in our provided solutions.
2. Real-World Applications
3. Visual Intuition
Visualizing the logic flow for Copy List With Random Pointer.
4. Prerequisites
5. Step-by-Step Thinking
1. Understand the problem
Read the problem statement for Copy List With Random Pointer carefully.
2. Formulate brute force
Draft a simple iterative solution.
3. Identify inefficiency
Look for redundant calculations.
4. Optimize search path
Use hashing or sorting to speed up the process.
5. Final Implementation
実稼働標準に合わせてコードをクリーンアップします。 ---パイセップ--- 空の入力構造体 ---パイセップ--- 単一要素入力 ---パイセップ--- 大きな数値限界 ---パイセップ--- リンク リスト アプローチのロジックを説明してください。 ---パイセップ--- null または空の入力などの特殊なケースについて説明します。 ---パイセップ--- 標準のリンク リストの問題プロパティが適用されます。 ---パイセップ--- セットやヒープなどのリンク リスト固有のデータ構造の使用を検討してください。 ---パイセップ--- 2 つの数値を加算する ---パイセップ--- 「2 つの数値の加算」問題の詳細なガイドと __PYTERM_0__ の実装。 ---パイセップ--- 2 つの非負の整数を表す 2 つの空ではないリンク リストが与えられます。数字は逆の順序で格納され、各ノードには 1 つの数字が含まれます。 2 つの数値を加算し、合計をリンク リストとして返します。 2 つの数値には、数値 0 自体を除き、先頭にゼロが含まれていないと想定できます。 リンクされたリストは __PYTERM_1__ リストとして表されます。合計を逆桁順のリストとして返す関数 __PYCODE_0__ を実装します。 ---パイセップ--- トップ150インタビュー ---パイセップ--- リンクされたリスト ---パイセップ--- 「2 つの数字を追加する」問題は、リンク リスト セクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。 ---パイセップ--- アルゴリズム工学 ---パイセップ--- 競技プログラミング ---パイセップ--- 技術的評価 ---パイセップ--- 2 つの数値を加算するためのロジック フローを視覚化します。 ---パイセップ--- 2 つの数値の足し算の問題文を注意深く読んでください。 ---パイセップ--- 単純な反復ソリューションの草案を作成します。 ---パイセップ--- 冗長な計算を探します。 ---パイセップ--- プロセスを高速化するには、ハッシュまたはソートを使用します。 ---パイセップ--- 実稼働標準に合わせてコードをクリーンアップします。 ---パイセップ--- 空の入力構造体 ---パイセップ--- 単一要素入力 ---パイセップ--- 大きな数値限界 ---パイセップ--- リンク リスト アプローチのロジックを説明してください。 ---パイセップ--- null または空の入力などの特殊なケースについて説明します。 ---パイセップ--- 標準のリンク リストの問題プロパティが適用されます。 ---パイセップ--- セットやヒープなどのリンク リスト固有のデータ構造の使用を検討してください。 ---パイセップ--- リンクリストサイクル ---パイセップ--- 「リンク リスト サイクル」問題の詳細なガイドと __PYTERM_0__ 実装。 ---パイセップ--- head (リンク リストの先頭) を指定すると、リンク リストにサイクルがあるかどうかが判断されます。 次のポインタを継続的にたどることによって再び到達できるノードがリスト内にある場合、リンクされたリスト内にサイクルが存在します。内部的には、pos は、tail の次のポインタが接続されているノードのインデックスを示すために使用されます。 pos はパラメータとして渡されないことに注意してください。 リンクされたリストに循環がある場合は true を返します。それ以外の場合は false を返します。 入力は、値のリストと整数 pos (末尾が接続されているインデックス、サイクルがない場合は -1) として指定されます。関数 __PYCODE_0__ を実装します。 ---パイセップ--- トップ150インタビュー ---パイセップ--- リンクされたリスト ---パイセップ--- 「リンク リスト サイクル」問題は、リンク リスト セクションの重要な課題です。 ---パイセップ--- この実装は、__PYTERM_0__ の簡単なレベルのロジックに焦点を当てています。 ---パイセップ--- 当社は、提供するソリューションにおいて技術的な正確さとコードの読みやすさを優先します。
問題提起
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 of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state.
The list is represented as a list of pairs [val, random_index] where random_index is the index of the node the random pointer points to, or -1 if it points to null. Implement a function copyRandomList(head: list) -> list that returns the deep copy in the same format.
- •0 <= n <= 1000
- •-10000 <= Node.val <= 10000
- •Node.random is null or points to some node in the linked list
例
[[7,-1],[13,0],[11,4],[10,2],[1,0]]
[[7,-1],[13,0],[11,4],[10,2],[1,0]]
The deep copy has the same structure. Node 0 (val=7) has random=null, Node 1 (val=13) has random pointing to Node 0, etc.
[[1,1],[2,1]]
[[1,1],[2,1]]
Node 0 (val=1) has random pointing to Node 1. Node 1 (val=2) has random pointing to Node 1 (itself).
[[3,-1],[3,0],[3,-1]]
[[3,-1],[3,0],[3,-1]]
Three nodes all with value 3. Node 1's random points to Node 0.
Need a Hint?
Edge Cases to Watch
- Empty input structures
- Single element inputs
- Large numerical bounds
解決する準備はできましたか?
Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.
インタビューの洞察とバリエーション
複雑さの分析の内訳
なぜ時間がかかるのか: Directly evaluates all possibilities.
なぜ宇宙なのか: Uses standard local memory.
なぜ時間がかかるのか: Optimized paths reduce total operations.
なぜ宇宙なのか: May trade memory for speed.
最適化されたソリューションの Python コード
最適化されたソリューションの Python コード
def copy_random_list_opt(head):
if not head: return None
if isinstance(head, list):
# Already in list format, return a deep copy
import copy
return copy.deepcopy(head)
return headブルート フォース コード (スポイラーガード付き)
ブルート フォース コード (スポイラーガード付き)
def copy_random_list_brute(head):
if not head: return None
# Map node indices to list of pairs format
if isinstance(head, list):
# Already in list format, return a deep copy
import copy
return copy.deepcopy(head)
return headAlgorithm Pattern Checklist
When dealing with Linked List data patterns.
- Are constraints clear?
- Is there a linear or logarithmic optimization possible?
Key Revision Notes
Standard Linked List problem properties apply.
関連する質問
PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!
推奨される Python リソース
関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。
Python リスト
Python リストについてすべて学びましょう。 Python で配列をネイティブに作成、スライス、変更、反復処理する方法を学びます。
Python でリストを並べ替える方法
sort() メソッドとsorted() 関数を使用して、Python でリストを並べ替える方法を学びます。カスタムキーの並べ替えと逆順の例をご覧ください。
Python リスト メソッドのチートシート
Python のリスト操作のクイック リファレンス ガイド。要素の追加、挿入、削除、並べ替え、スライスをマスターします。
Python と JavaScript: どちらのプログラミング言語が最適ですか?
Python と JavaScript の包括的な比較。構文の違い、パフォーマンス、使用例 (バックエンドとフロントエンド)、およびコーディング例を調べます。