Palindrome Linked List in Python

A palindrome linked list is a linked list that reads the same forwards and backwards. For example, [1,2,3,2,1] forms a palindrome because it's identical when reversed.

The most efficient approach uses the two-pointer technique with partial reversal to check palindrome in O(n) time and O(1) space.

Algorithm Steps

  • Use fast and slow pointers to find the middle of the linked list

  • Reverse the first half while traversing to the middle

  • Compare the reversed first half with the second half

  • Return True if all elements match, False otherwise

Implementation

class ListNode:
    def __init__(self, data, next=None):
        self.data = data
        self.next = next

def make_list(elements):
    if not elements:
        return None
    head = ListNode(elements[0])
    current = head
    for element in elements[1:]:
        current.next = ListNode(element)
        current = current.next
    return head

class Solution:
    def isPalindrome(self, head):
        if not head or not head.next:
            return True
        
        fast = slow = head
        prev = None
        
        # Find middle and reverse first half
        while fast and fast.next:
            fast = fast.next.next
            
            # Reverse the first half
            temp = slow
            slow = slow.next
            temp.next = prev
            prev = temp
        
        # Handle odd length lists
        if fast:  # Odd length
            slow = slow.next
        
        # Compare reversed first half with second half
        while prev and slow:
            if prev.data != slow.data:
                return False
            prev = prev.next
            slow = slow.next
        
        return True

# Test the implementation
elements = [1, 2, 3, 2, 1]
head = make_list(elements)
solution = Solution()
print(f"Is [{', '.join(map(str, elements))}] a palindrome? {solution.isPalindrome(head)}")

# Test with non-palindrome
elements2 = [1, 2, 3, 4, 5]
head2 = make_list(elements2)
print(f"Is [{', '.join(map(str, elements2))}] a palindrome? {solution.isPalindrome(head2)}")
Is [1, 2, 3, 2, 1] a palindrome? True
Is [1, 2, 3, 4, 5] a palindrome? False

How It Works

The algorithm uses two pointers moving at different speeds:

  • Fast pointer: Moves 2 steps at a time to find the end

  • Slow pointer: Moves 1 step at a time, reaching the middle when fast reaches the end

  • While slow moves, we reverse the first half by adjusting the next pointers

  • After reaching the middle, we compare the reversed first half with the second half

Time and Space Complexity

Aspect Complexity Explanation
Time O(n) Single pass through the linked list
Space O(1) Only uses a few pointer variables

Conclusion

This approach efficiently checks if a linked list is a palindrome by using the two-pointer technique with partial reversal. It achieves optimal O(n) time complexity with O(1) space complexity, making it ideal for large linked lists.

Updated on: 2026-03-25T07:30:20+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements