Palindrome Linked List

Check if a linked list is a palindrome

Read Here
Step0 / 10
PhaseInitialize
Nodes5
Result
Back To Linked List

Code

1def isPalindrome(head):
2 if not head or not head.next:
3 return True
4
5 # Find middle using slow/fast pointers
6 slow = fast = head
7 while fast and fast.next:
8 slow = slow.next
9 fast = fast.next.next
10
11 # Reverse second half
12 prev = None
13 curr = slow
14 while curr:
15 next_node = curr.next
16 curr.next = prev
17 prev = curr
18 curr = next_node
19
20 # Compare both halves
21 left = head
22 right = prev
23 while right:
24 if left.val != right.val:
25 return False
26 left = left.next
27 right = right.next
28
29 return True
Function definition

Linked List

INIT

Step

0 / 10

Phase

Initialize

Nodes

5

null1next2next3next2next1nextslowfast

Progress

Processing

Checking if the linked list is a palindrome...

Pointer State

Pointers

slow

1

fast

1

left

null

right

null

Key Insight

Find the middle with slow/fast pointers, reverse the second half, then compare from both ends. No extra space needed.

Explanation

Setup

Initialize Pointers

Set both slow and fast pointers to the head of the list.

💡 Tip

Slow moves 1 step, fast moves 2 steps per iteration.