Talentd
FleetCode

234. Palindrome Linked List

Easy58.8% acceptance17 companies

Given the head of a singly linked list, return true if it is a palindrome or false otherwise.

Example 1:

Input: head = [1,2,2,1]
Output: true

Example 2:

Input: head = [1,2]
Output: false

Constraints:

  • The number of nodes in the list is in the range [1, 105].
  • 0 <= Node.val <= 9
Follow up: Could you do it in O(n) time and O(1) space?
11:06
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1# Definition for singly-linked list.
2# class ListNode:
3# def __init__(self, val=0, next=None):
4# self.val = val
5# self.next = next
6class Solution:
7 def isPalindrome(self, head: Optional[ListNode]) -> bool:
8
Starter codePython 3Spaces: 4
Examples
Input
[1,2,2,1]
Input
[1,2]
Your code is saved on this device.Solution