-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdetect_cycle_hashing.rb
47 lines (37 loc) · 1.09 KB
/
detect_cycle_hashing.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
# Input: head = [3,2,0,-4], pos = 1
# Output: tail connects to node index 1
# Explanation: There is a cycle in the linked list, where tail connects to the second node.
require_relative './linklist'
# using hashing
def detect_cycle_hasing(head)
return false if head.nil?
current = head
visited = { current.object_id => 0 }
index = 1
while current.next
puts visited
return "tail connects to node index #{visited[current.next.object_id]}" if visited[current.next.object_id]
visited[current.next.object_id] = index
current = current.next
index += 1
end
'no cycle'
end
# LeetCode submission
def detectCycle(head)
return nil if head.nil?
current = head
visited = { current.object_id => 0 }
index = 1
while current.next
return current.next if visited[current.next.object_id]
visited[current.next.object_id] = index
current = current.next
index += 1
end
nil
end
ll = LinkList.new([1, 2, 3, 4, 5, 6], 5)
# ll.print
puts detect_cycle_hasing(ll.head)