Palindrome Linked List
Example
Solution
1)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# @param head, a ListNode
# @return a boolean
def isPalindrome(self, head):
# Write your code here
if head == None or head.next == None:
return True
list = []
fast = head
slow = head
list.append(head.val)
while fast.next and fast.next.next:
fast = fast.next.next
slow = slow.next
list.append(slow.val)
if fast.next == None:
list.pop()
while slow.next:
slow = slow.next
if slow.val != list.pop():
return False
return TrueLast updated