[234] Palindrome Linked List
https://leetcode.com/problems/palindrome-linked-list/description/
- algorithms
- Easy (34.56%)
- Source Code: 234.palindrome-linked-list.py
- Total Accepted: 246.9K
- Total Submissions: 691.1K
- Testcase Example: '[1,2]'
Given a singly linked list, determine if it is a palindrome.
Example 1:
Input: 1->2 Output: false
Example 2:
Input: 1->2->2->1 Output: true
Follow up: Could you do it in O(n) time and O(1) space?
python
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
def isPalindrome(self, head):
"""
:type head: ListNode
:rtype: bool
"""
if not head or not head.next: return True
lst = []
p = head
while p:
lst.append(p.val)
p = p.next
lenn = len(lst)
l, r = 0, lenn-1
while l < r:
if not lst[l] == lst[r]: return False
l += 1
r -= 1
return True