Created
April 2, 2018 02:55
-
-
Save jsm/125e5dfbbca4204200b335319a7ba25d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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