Palindrome Linked List

Easy

Topics
Linked ListTwo PointersStack

Given the head of a singly linked list, return true if the sequence of values reads the same forwards and backwards. (The judge passes a plain acyclic list; pos is always -1.)

Example 1

1221null
second half, reversed in place
The values read the same from head to tail as from tail to head, so the answer is true. What makes this more than a string comparison is that a singly linked list can only be walked forwards; there is no way to step back from the tail.
Linked list values from head to tail.
Input:  head = [1,2,2,1]
Output: true

Example 2

12null
mismatch
Read head to tail and tail to head, the values differ — the shaded pair is the first disagreement — so the answer is false.
Input:  head = [1,2]
Output: false

Constraints

  • 0 <= number of nodes <= 100
  • 0 <= Node.val <= 9
Editorial — intuition, approach, complexity and pitfalls

Intuition

A linked list can only be walked forwards, so comparing the first element to the last is not a thing you can do directly. Copying the values into an array makes the list randomly accessible, after which it is the ordinary two-pointer palindrome check. That is O(n) time and O(n) space, and is what the shown solution does.

The O(1)-space answer is the standard follow-up and worth knowing: find the middle with slow/fast pointers, reverse the second half in place, compare the two halves node by node, then reverse it back so the caller's list is unchanged. Same O(n) time, no allocation.

The reason it is more than a trick: reversing the second half turns "walk backwards from the end" into "walk forwards from the middle", which is the only way a singly linked list can be traversed.

Approach

  1. Walk the list, appending values to a list.
  2. Compare it to its reverse.

Dry Run

1 -> 2 -> 2 -> 1
values [1,2,2,1]
reversed [1,2,2,1]   equal -> true

1 -> 2
values [1,2]  vs  [2,1]  -> false

Complexity

  • Time — O(n): one walk plus one comparison.
  • Space — O(n): the value array. The reverse-half variant is O(1), which is the version to offer when asked — and it is worth mentioning that it mutates the list mid-flight, so restoring it matters if anything else holds a reference.

Common Pitfalls

  • Comparing node objects rather than their values. Distinct nodes holding equal values must compare equal.
  • In the O(1) variant, failing to handle an odd length. The middle node belongs to neither half and must be skipped.
  • In the O(1) variant, leaving the list reversed. The function is a predicate and should not have side effects.
  • Treating the empty list as not a palindrome. Zero and one nodes are both trivially palindromes, and the constraints here allow zero.

Full Solution — all languages

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def isPalindrome(self, head):
        vals = []
        while head:
            vals.append(head.val)
            head = head.next
        return vals == vals[::-1]
SensAI
Beta
Tap the mic to stop voice mode