-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.rb
More file actions
54 lines (33 loc) · 693 Bytes
/
Solution.rb
File metadata and controls
54 lines (33 loc) · 693 Bytes
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
# Definition for singly-linked list.
class ListNode
attr_accessor :val, :next
def initialize(val)
@val = val
@next = nil
end
end
# @param {ListNode} head
# @return {Boolean}
def is_palindrome(head)
if head.nil? or head.next.nil?
return true
end
value_array = Array.new
ptr = head
until ptr.nil?
value_array << ptr.val
ptr = ptr.next
end
left = 0; right = value_array.size - 1
while left < right
if value_array[left] != value_array[right]
return false
end
left += 1; right -= 1
end
true
end
head = ListNode.new 1
head.next = ListNode.new 2
head.next.next = ListNode.new 1
puts is_palindrome head