|
| 1 | +[](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby) |
| 2 | +[](https://github.com/LeetCode-in-Ruby/LeetCode-in-Ruby/fork) |
| 3 | + |
| 4 | +## 138\. Copy List with Random Pointer |
| 5 | + |
| 6 | +Medium |
| 7 | + |
| 8 | +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`. |
| 9 | + |
| 10 | +Construct a [**deep copy**](https://en.wikipedia.org/wiki/Object_copying#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. **None of the pointers in the new list should point to nodes in the original list**. |
| 11 | + |
| 12 | +For example, if there are two nodes `X` and `Y` in the original list, where `X.random --> Y`, then for the corresponding two nodes `x` and `y` in the copied list, `x.random --> y`. |
| 13 | + |
| 14 | +Return _the head of the copied linked list_. |
| 15 | + |
| 16 | +The linked list is represented in the input/output as a list of `n` nodes. Each node is represented as a pair of `[val, random_index]` where: |
| 17 | + |
| 18 | +* `val`: an integer representing `Node.val` |
| 19 | +* `random_index`: the index of the node (range from `0` to `n-1`) that the `random` pointer points to, or `null` if it does not point to any node. |
| 20 | + |
| 21 | +Your code will **only** be given the `head` of the original linked list. |
| 22 | + |
| 23 | +**Example 1:** |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +**Input:** head = \[\[7,null],[13,0],[11,4],[10,2],[1,0]] |
| 28 | + |
| 29 | +**Output:** [[7,null],[13,0],[11,4],[10,2],[1,0]] |
| 30 | + |
| 31 | +**Example 2:** |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +**Input:** head = \[\[1,1],[2,1]] |
| 36 | + |
| 37 | +**Output:** [[1,1],[2,1]] |
| 38 | + |
| 39 | +**Example 3:** |
| 40 | + |
| 41 | +**** |
| 42 | + |
| 43 | +**Input:** head = \[\[3,null],[3,0],[3,null]] |
| 44 | + |
| 45 | +**Output:** [[3,null],[3,0],[3,null]] |
| 46 | + |
| 47 | +**Example 4:** |
| 48 | + |
| 49 | +**Input:** head = [] |
| 50 | + |
| 51 | +**Output:** [] |
| 52 | + |
| 53 | +**Explanation:** The given linked list is empty (null pointer), so return null. |
| 54 | + |
| 55 | +**Constraints:** |
| 56 | + |
| 57 | +* `0 <= n <= 1000` |
| 58 | +* `-10000 <= Node.val <= 10000` |
| 59 | +* `Node.random` is `null` or is pointing to some node in the linked list. |
| 60 | + |
| 61 | +## Solution |
| 62 | + |
| 63 | +```ruby |
| 64 | +require_relative '../../com_github_leetcode/random/node' |
| 65 | + |
| 66 | +# Definition for Node. |
| 67 | +# class Node |
| 68 | +# attr_accessor :val, :next, :random |
| 69 | +# def initialize(val = 0) |
| 70 | +# @val = val |
| 71 | +# @next = nil |
| 72 | +# @random = nil |
| 73 | +# end |
| 74 | +# end |
| 75 | + |
| 76 | +# @param {Node} node |
| 77 | +# @return {Node} |
| 78 | +def copy_random_list(head) |
| 79 | + return nil if head.nil? |
| 80 | + |
| 81 | + # First pass to create cloned nodes and insert them after the original nodes |
| 82 | + curr = head |
| 83 | + while curr |
| 84 | + cloned_node = Node.new(curr.val) |
| 85 | + cloned_node.next = curr.next |
| 86 | + curr.next = cloned_node |
| 87 | + curr = cloned_node.next |
| 88 | + end |
| 89 | + |
| 90 | + # Second pass to set the random pointers of the cloned nodes |
| 91 | + curr = head |
| 92 | + while curr |
| 93 | + curr.next.random = curr.random.next if curr.random |
| 94 | + curr = curr.next.next |
| 95 | + end |
| 96 | + |
| 97 | + # Third pass to separate the original and cloned nodes |
| 98 | + curr = head |
| 99 | + new_head = nil |
| 100 | + while curr |
| 101 | + cloned_node = curr.next |
| 102 | + new_head ||= cloned_node |
| 103 | + curr.next = cloned_node.next |
| 104 | + cloned_node.next = curr.next.next if curr.next |
| 105 | + curr = curr.next |
| 106 | + end |
| 107 | + |
| 108 | + new_head |
| 109 | +end |
| 110 | +``` |
0 commit comments