Palindrome Linked List solution explained

To check whether a linked list is a palindrome in O(1) extra space, find the end of the first half with a slow and a fast pointer, reverse the second half in place, compare the two halves node by node, and reverse the second half back before returning. Copying the values into an array and comparing it with its reverse is simpler but uses O(n) memory. Both versions of the Palindrome Linked List solution run in O(n) time.

By Published 6 min read

The task, restated

Given the head of a singly linked list of values, decide whether the values form a palindrome: the first equals the last, the second equals the second to last, and so on. You can only move forward through next pointers. The follow-up asks for O(n) time and O(1) extra memory.

Lists checked for this page
ListPalindrome?Note
4 → 9 → 9 → 4YesEven length: halves are 4, 9 and 9, 4
3 → 7 → 2 → 7 → 3YesOdd length: the middle 2 is compared with nothing
5 → 1 → 5 → 1NoFirst 5 meets last 1
6YesA single node
emptyYesUsually treated as a palindrome; confirm
Lists checked for this page. Scroll sideways on a small screen.

Two things to settle: what an empty list should return (true is the common choice) and whether you are allowed to modify the list. The constant-space approach changes pointers temporarily, so if the list is shared with other code, say that you will put it back, and do.

How interviewers use it

The array version takes a minute, so the interview really starts at the follow-up. Interviewers want to see that you can combine two building blocks, finding the middle and reversing a list, without off-by-one trouble on odd and even lengths. Restoring the list afterwards is a quality signal many candidates skip.

  • Do it without modifying the list. Then you need O(n) space: an array, a stack of the first half, or recursion (which uses the call stack).
  • Compare by a key, such as case-insensitive characters. Only the comparison line changes.
  • Check a doubly linked list. Walk inward from both ends; no reversal needed.
  • Return the first mismatching pair. Record the pair at the moment the comparison fails.

Brute force: copy values to an array

def is_palindrome_copy(head):
    vals = []
    while head:
        vals.append(head.val)
        head = head.next
    return vals == vals[::-1]
Python baseline: O(n) time and O(n) space, and the list is never touched.

This is correct and is the right answer if the list must stay read-only. Mention it first, then offer the constant-space version.

O(1) space: reverse the back half, compare, restore

  1. Find the end of the first halfMove slow one step and fast two steps while fast has two more nodes ahead. Slow stops on the last node of the first half (the middle node for odd lengths).
  2. Reverse the second halfReverse the list that starts at slow.next. Its new head is the original last node.
  3. CompareWalk from the original head and from the reversed half together until the reversed half runs out. Any difference means not a palindrome.
  4. RestoreReverse the second half again and attach it to slow.next, so the caller gets the original list back.
def reverse(node):
    prev = None
    while node:
        nxt = node.next
        node.next = prev
        prev, node = node, nxt
    return prev

def is_palindrome(head):
    if head is None or head.next is None:
        return True
    first_end = fast = head
    while fast.next and fast.next.next:
        first_end = first_end.next
        fast = fast.next.next
    second = reverse(first_end.next)
    a, b, same = head, second, True
    while b:
        if a.val != b.val:
            same = False
            break
        a, b = a.next, b.next
    first_end.next = reverse(second)  # put the list back
    return same
Python: fast/slow middle, in-place reversal, and a restore step.
function reverseList(node) {
  let prev = null;
  while (node) {
    const nxt = node.next;
    node.next = prev;
    prev = node;
    node = nxt;
  }
  return prev;
}

function isPalindrome(head) {
  if (!head || !head.next) return true;
  let firstEnd = head, fast = head;
  while (fast.next && fast.next.next) {
    firstEnd = firstEnd.next;
    fast = fast.next.next;
  }
  const second = reverseList(firstEnd.next);
  let a = head, b = second, same = true;
  while (b) {
    if (a.val !== b.val) { same = false; break; }
    a = a.next;
    b = b.next;
  }
  firstEnd.next = reverseList(second);
  return same;
}
JavaScript: the same four steps.

Why the comparison loop runs on b: the reversed half is never longer than the first half. For 3 → 7 → 2 → 7 → 3, the first half ends at 2 and the reversed back half is 3 → 7, so the middle value is never compared, which is exactly right. Breaking out of the loop instead of returning early is what lets the restore step always run.

Time and space

Complexity for a list of n nodes
ApproachTimeExtra spaceModifies the list?
Copy to arrayO(n)O(n)No
Stack of the first halfO(n)O(n/2)No
Recursive comparisonO(n)O(n) call stackNo
Reverse the back halfO(n), a few linear passesO(1)Temporarily, then restored
Complexity for a list of n nodes. Scroll sideways on a small screen.

Edge cases

  • Empty list and one node: true without any pointer work.
  • Two nodes, equal or different: the first half is one node, the second half is one node.
  • Odd length: the middle node belongs to the first half and is not compared.
  • Values that are equal but different objects, such as strings in some languages: compare by value, not identity.

Mistakes to avoid

  • Using fast and fast.next as the loop condition here, which puts slow one node later on even lengths and changes what "second half" means.
  • Returning false from inside the loop, so the list is left half-reversed.
  • Comparing until a reaches null; the first half still links into the old middle and can run past the end of the reversed half.
  • Reversing the whole list instead of half, then comparing the list with itself.
  • Claiming O(1) space for the recursive version, which uses n stack frames.

How to explain it out loud

Fictional example

Moving from the easy answer to the follow-up
Candidate
The simple way is to copy the values into an array and compare it with its reverse. Linear time and linear space, and the list is untouched.
Candidate
For constant space, I find where the first half ends with a slow and a fast pointer, reverse the second half, and compare from both starts.
Candidate
I break instead of returning on a mismatch, then reverse the back half again and reconnect it, so the caller's list is unchanged.

Intersection of Two Linked Lists is another two-pointer list problem, where the pointers walk different lists. Odd Even Linked List practices splitting one list into two chains. Valid Parentheses is a different symmetry check, solved with a stack.

Where Natively fits

During practice on Mac or Windows, Natively can read a problem like this from a screenshot and write an approach and code in the overlay; it is most useful after you have attempted the restore step yourself. Transcribing your mock round lets you check whether you offered the array version before the constant-space one. Details are on the AI interview assistant page.

Questions people ask

How do you check if a linked list is a palindrome?

Copy the values to an array and compare it with its reverse, or for O(1) space find the middle with slow and fast pointers, reverse the second half, and compare the halves node by node.

Can you check a palindrome linked list in O(1) space?

Yes. Reverse the second half in place, compare it with the first half, then reverse it again so the list is unchanged. Time stays O(n).

What is the time complexity of Palindrome Linked List?

O(n) time for every approach. The array copy uses O(n) memory; the reverse-half method uses O(1).

What happens to the middle node in an odd-length list?

It belongs to the first half and is never compared, because it matches itself.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP