-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathis_palindrome3.rb
69 lines (59 loc) · 1.61 KB
/
is_palindrome3.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# https://leetcode.com/problems/palindrome-linked-list/
# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val = 0, _next = nil)
# @val = val
# @next = _next
# end
# end
# 3. Reverse Second Half In-place
# this is a solution with space complexity of O(1)
# Find the middle of the list
# Reverse the second half in place
# then with two pointers - one starting at the first element and one at the beginning of the second half
# check if the values are the same
# afterwards put the secon half back into the correct order
def is_palindrome?(head)
return true if head.nil?
# Find the end of first half and reverse second half.
first_half_end = end_of_first_half(head)
second_half_start = reverse_list(first_half_end.next)
# Check whether or not there is a palindrome.
p1 = head
p2 = second_half_start
result = true
while result && !p2.nil?
if p1.val != p2.val
result = false
end
p1 = p1.next
p2 = p2.next
end
# Restore the list and return the result.
first_half_end.next = reverse_list(second_half_start)
result
end
def reverse_list(head)
prev = nil
cur = head
while !cur.nil?
next_temp = cur.next
cur.next = prev
prev = cur
cur = next_temp
end
return prev
end
def end_of_first_half(node)
# two pointers: 'slow' will move one node at the time
# 'fast' will move two
# once 'fast' reaches the end, 'slow' will be in the middle
slow = node
fast = node
while !fast.next.nil? && !fast.next.next.nil?
fast = fast.next.next
slow = slow.next
end
return slow
end