Leetcode 234 Palindrome Linked List
Follow up: Could you do it in O(n) time and O(1) space?
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
def isPalindrome(self, head: ListNode) -> bool:
fast = head
slow = head
#orta elemanı bulalım(slow)
while fast and fast.next:
fast = fast.next.next
slow = slow.next
#2. parçayı terse çevir
prev = None
while slow:
tmp = slow.next
slow.next = prev
prev = slow
slow = tmp
#palindrome kontrol edelim
left,right = head,prev
while right:
if left.val != right.val:
return False
left = left.next
right = right.next
return True