Palindrome Linked List
Check if a linked list is a palindrome
Code
1def isPalindrome(head):2 if not head or not head.next:3 return True45 # Find middle using slow/fast pointers6 slow = fast = head7 while fast and fast.next:8 slow = slow.next9 fast = fast.next.next1011 # Reverse second half12 prev = None13 curr = slow14 while curr:15 next_node = curr.next16 curr.next = prev17 prev = curr18 curr = next_node1920 # Compare both halves21 left = head22 right = prev23 while right:24 if left.val != right.val:25 return False26 left = left.next27 right = right.next2829 return True
Function definition
Linked List
INIT
Step
0 / 10
Phase
Initialize
Nodes
5
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.