Skip to content

Instantly share code, notes, and snippets.

@jsm
Created April 2, 2018 02:55
Show Gist options
  • Save jsm/125e5dfbbca4204200b335319a7ba25d to your computer and use it in GitHub Desktop.
Save jsm/125e5dfbbca4204200b335319a7ba25d to your computer and use it in GitHub Desktop.
# Definition for singly-linked list:
# class ListNode
# attr_accessor :value, :next
# def initialize(val)
# @value = val
# @next = nil
# end
#
def isListPalindrome(l)
offset = -1
curr = l
while curr
offset += 1
curr = curr.next
end
tracker = {}
pos = 0
curr = l
while curr
tracker[curr.value] ||= 0
tracker[curr.value] = tracker[curr.value] ^ (pos-offset).abs
pos += 2
curr = curr.next
end
puts tracker
return !tracker.any? do |k, v|
v != 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment