Palindrome Linked List

Published: Sep 12, 2022

Easy Linked List Two Pointers


A brute force solution would be saving values to an array, then compared both start and end. Another solutions is reversing first half and compares the values.

Problem Description

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


  • The number of nodes in the list is in the range [1, 10**5].
  • 0 <= Node.val <= 9


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


The solution here reverses the first half of the linked list. It uses three pointers, slow, fast, and reversed. When the fast pointer reaches to the end, the first half is reversed. The slow pointer is on the middle. If the length of the linked list is odd, fast pointer has a next node still. In this case, slow pointer is incremented by one. The comparison ends when revered pointer reaches to the end.


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
# = next
class Solution:
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        fast, slow, rev = head, head, None
        while fast and
            fast =
            rev,, slow = slow, rev,
        if fast:
            slow =
        while rev:
            if rev.val != slow.val:
                return False
            rev, slow =,
        return True


  • Time: O(n)
  • Space: O(1)