Easy
Given the head
of a singly linked list, return true
if it is a palindrome.
Example 1:
Input: head = [1,2,2,1]
Output: true
Example 2:
Input: head = [1,2]
Output: false
Constraints:
[1, 105]
.0 <= Node.val <= 9
Follow up: Could you do it in O(n)
time and O(1)
space?
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
arr = []
while head:
arr.append(head.val)
head = head.next
l = 0
r = len(arr) -1
while l <= r:
if arr[l] != arr[r]:
return False
l+=1
r-=1
return True