Skip to content

Commit dbc262e

Browse files
authored
Merge pull request #713 from fartem/138_Copy_List_with_Random_Pointer
2024-08-20 v. 6.5.1: added "138. Copy List with Random Pointer"
2 parents 8fa4104 + 13607ae commit dbc262e

File tree

5 files changed

+132
-1
lines changed

5 files changed

+132
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,3 +516,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
516516
| 129. Sum Root to Leaf Numbers | [Link](https://leetcode.com/problems/sum-root-to-leaf-numbers/) | [Link](./lib/medium/129_sum_root_to_leaf_numbers.rb) |
517517
| 133. Clone Graph | [Link](https://leetcode.com/problems/clone-graph/) | [Link](./lib/medium/133_clone_graph.rb) |
518518
| 134. Gas Station | [Link](https://leetcode.com/problems/gas-station/) | [Link](./lib/medium/134_gas_station.rb) |
519+
| 138. Copy List with Random Pointer | [Link](https://leetcode.com/problems/copy-list-with-random-pointer/) | [Link](./lib/medium/138_copy_list_with_random_pointer.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '6.5.0'
8+
s.version = '6.5.1'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/common/list_node_with_random.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# frozen_string_literal: true
2+
3+
# ListNodeWithRandom implementation for this project
4+
class ListNodeWithRandom
5+
attr_accessor :val, :next, :random
6+
7+
# @param {Integer} val
8+
# @param {ListNodeWithRandom} nxt
9+
# @param {ListNodeWithRandom} random
10+
def initialize(val, nxt = nil, random = nil)
11+
@val = val
12+
@next = nxt
13+
@random = random
14+
end
15+
16+
# @param {ListNodeWithRandom} l1
17+
# @param {ListNodeWithRandom} l2
18+
# @return {Boolean}
19+
def self.are_equal(l1, l2)
20+
while l1 && l2
21+
return false if l1.random && l2.random && l1.random.val != l2.random.val
22+
23+
return false if l1.val != l2.val
24+
25+
l1 = l1.next
26+
l2 = l2.next
27+
end
28+
29+
l1.nil? && l2.nil?
30+
end
31+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../common/list_node_with_random'
4+
5+
# https://leetcode.com/problems/copy-list-with-random-pointer/
6+
# @param {Node} node
7+
# @return {Node}
8+
def copy_random_list(head)
9+
nodes = {}
10+
p = head
11+
while p
12+
nodes[p] = ::ListNodeWithRandom.new(p.val)
13+
p = p.next
14+
end
15+
16+
p = head
17+
while p
18+
node = nodes[p]
19+
node.next = nodes[p.next]
20+
node.random = nodes[p.random]
21+
p = p.next
22+
end
23+
24+
nodes[head]
25+
end
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/list_node_with_random'
5+
require_relative '../../lib/medium/138_copy_list_with_random_pointer'
6+
require 'minitest/autorun'
7+
8+
class CopyListWithRandomPointerTest < ::Minitest::Test
9+
def test_default_one
10+
first = ::ListNodeWithRandom.new(7)
11+
second = ::ListNodeWithRandom.new(13)
12+
third = ::ListNodeWithRandom.new(11)
13+
fourth = ::ListNodeWithRandom.new(10)
14+
fifth = ::ListNodeWithRandom.new(1)
15+
16+
first.next = second
17+
second.next = third
18+
third.next = fourth
19+
fourth.next = fifth
20+
21+
second.random = first
22+
third.random = fifth
23+
fourth.random = third
24+
fifth.random = first
25+
26+
assert(
27+
::ListNodeWithRandom.are_equal(
28+
first,
29+
copy_random_list(
30+
first
31+
)
32+
)
33+
)
34+
end
35+
36+
def test_default_two
37+
first = ::ListNodeWithRandom.new(1)
38+
second = ::ListNodeWithRandom.new(2)
39+
40+
first.next = second
41+
42+
first.random = second
43+
second.random = second
44+
45+
assert(
46+
::ListNodeWithRandom.are_equal(
47+
first,
48+
copy_random_list(
49+
first
50+
)
51+
)
52+
)
53+
end
54+
55+
def test_default_three
56+
first = ::ListNodeWithRandom.new(3)
57+
second = ::ListNodeWithRandom.new(3)
58+
third = ::ListNodeWithRandom.new(3)
59+
60+
first.next = second
61+
second.next = third
62+
63+
third.random = first
64+
65+
assert(
66+
::ListNodeWithRandom.are_equal(
67+
first,
68+
copy_random_list(
69+
first
70+
)
71+
)
72+
)
73+
end
74+
end

0 commit comments

Comments
 (0)