File tree 5 files changed +131
-1
lines changed
5 files changed +131
-1
lines changed Original file line number Diff line number Diff line change @@ -516,3 +516,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
516
516
| 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 ) |
517
517
| 133. Clone Graph | [ Link] ( https://leetcode.com/problems/clone-graph/ ) | [ Link] ( ./lib/medium/133_clone_graph.rb ) |
518
518
| 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/gas-station/ ) | [ Link] ( ./lib/medium/138_copy_list_with_random_pointer.rb ) |
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ require 'English'
5
5
::Gem ::Specification . new do |s |
6
6
s . required_ruby_version = '>= 3.0'
7
7
s . name = 'leetcode-ruby'
8
- s . version = '6.5.0 '
8
+ s . version = '6.5.1 '
9
9
s . license = 'MIT'
10
10
s . files = ::Dir [ 'lib/**/*.rb' ] + %w[ README.md ]
11
11
s . executable = 'leetcode-ruby'
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../common/list_node_with_random'
4
+
5
+ # @param {Node} node
6
+ # @return {Node}
7
+ def copy_random_list ( head )
8
+ nodes = { }
9
+ p = head
10
+ while p
11
+ nodes [ p ] = ::ListNodeWithRandom . new ( p . val )
12
+ p = p . next
13
+ end
14
+
15
+ p = head
16
+ while p
17
+ node = nodes [ p ]
18
+ node . next = nodes [ p . next ]
19
+ node . random = nodes [ p . random ]
20
+ p = p . next
21
+ end
22
+
23
+ nodes [ head ]
24
+ end
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments