Visualize

Pattern visualizer

Palindrome Linked List

A palindrome reads identically forwards and backwards, but a singly-linked list can only be walked forward — there's no way to compare node i against node n-1-i without first turning the back half around. So physically reverse the second half in place (no extra array needed), then walk two pointers outward from the split comparing values one by one; any mismatch means it isn't a palindrome. Finding the middle first with slow/fast pointers means the length never has to be known in advance. Animated on: Check if list [1,2,2,1] is a palindrome: middle at idx1, reverse second half [2,1]→[1,2], compare [1,2] vs [1,2] → true.

Linked List

step 1 / 8
1
[0]
2
[1]
2
[2]
1
[3]
line 2

Input: list [1,2,2,1] (even length, palindrome). Slow/fast pointers start at head (idx0). Slow moves 1 step, fast moves 2 steps per iteration to find the middle.

Pseudocode
1FUNCTION isPalindrome(head):
2 IF the list is empty or has one node: RETURN true
3 slow = head; fast = head
4 WHILE fast can still take two steps: move slow one forward; move fast two forward
5 second = reverse(the node after slow) (reverse the back half)
6 first = head
7 WHILE second still has nodes: IF first's value is not equal to second's value RETURN false; move first forward; move second forward
8 RETURN true
9END FUNCTION
10FUNCTION reverse(head): prev = nothing; curr = head; WHILE curr has nodes: next = node after curr; point curr back to prev; prev = curr; curr = next; RETURN prev

← / → step · space play · Home restart

Where to practice Linked List