234. Palindrome Linked List
Input: 1->2
Output: falseInput: 1->2->2->1
Output: true# Definition for singly-linked list.
# class ListNode
# attr_accessor :val, :next
# def initialize(val = 0, _next = nil)
# @val = val
# @next = _next
# end
# end
# @param {ListNode} head
# @return {Boolean}
def is_palindrome(head)
return true if head.nil?
data = []
while(!head.nil?)
data << head.val
head = head.next
end
data == data.reverse
endLast updated