Skip to content

Commit e604312

Browse files
authored
Merge pull request #716 from fartem/142_Linked_List_Cycle_II
2024-08-21 v. 6.5.2: added "142. Linked List Cycle II"
2 parents e91eb4b + 709e42b commit e604312

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
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) |
519519
| 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) |
520+
| 142. Linked List Cycle II | [Link](https://leetcode.com/problems/linked-list-cycle-ii/) | [Link](./lib/medium/142_linked_list_cycle_ii.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.1.2'
8+
s.version = '6.5.2'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/linked-list-cycle-ii/
4+
# @param {ListNode} head
5+
# @return {ListNode}
6+
def detect_cycle(head)
7+
tort = head
8+
hare = head
9+
is_cycle = false
10+
while tort && hare && hare.next
11+
tort = tort.next
12+
hare = hare.next.next
13+
14+
next unless hare == tort
15+
16+
is_cycle = true
17+
18+
break
19+
end
20+
21+
return unless is_cycle
22+
23+
tort = head
24+
until tort == hare
25+
tort = tort.next
26+
hare = hare.next
27+
end
28+
29+
tort
30+
end
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/linked_list'
5+
require_relative '../../lib/medium/142_linked_list_cycle_ii'
6+
require 'minitest/autorun'
7+
8+
class LinkedListCycleIITest < ::Minitest::Test
9+
def test_default_one
10+
start = ::ListNode.new(2)
11+
middle = ::ListNode.new(0)
12+
start.next = middle
13+
middle.next = ::ListNode.new(-4, start)
14+
head = ::ListNode.new(3, start)
15+
16+
assert_equal(
17+
start,
18+
detect_cycle(
19+
head
20+
)
21+
)
22+
end
23+
24+
def test_default_two
25+
start = ::ListNode.new(1)
26+
nd = ::ListNode.new(2)
27+
start.next = nd
28+
nd.next = start
29+
30+
assert_equal(
31+
start,
32+
detect_cycle(
33+
start
34+
)
35+
)
36+
end
37+
38+
def test_default_three
39+
assert_nil(
40+
detect_cycle(
41+
::ListNode.new(1)
42+
)
43+
)
44+
end
45+
end

0 commit comments

Comments
 (0)