Palindrome Linked List in O(n) Time and O(1) Space using Python


Given the head of a singly linked list, return true if it is a palindrome.

 

Example 1:



Input: head = [1,2,2,1]
Output: true

Example 2:



Input: head = [1,2]
Output: false

 

Constraints:

  • The number of nodes in the list is in the range [1, 105].
  • 0 <= Node.val <= 9

 

Follow up: Could you do it in O(n) time and O(1) space?

Understanding the Problem

The core challenge of this problem is to determine if the values in a singly linked list form a palindrome. A palindrome is a sequence that reads the same backward as forward. This problem is significant in various applications such as text processing, data validation, and more.

Potential pitfalls include handling edge cases like an empty list or a list with a single node, and ensuring the solution is efficient in both time and space.

Approach

To solve this problem, we can consider the following approaches:

Naive Solution

A naive solution would involve copying the values of the linked list into an array and then checking if the array is a palindrome. This approach is straightforward but requires O(n) extra space.

Optimized Solution

To achieve O(n) time and O(1) space complexity, we can use the following approach:

  1. Find the middle of the linked list using the slow and fast pointer technique.
  2. Reverse the second half of the linked list.
  3. Compare the first half and the reversed second half of the list.
  4. Restore the original list structure (optional).

Algorithm

Let's break down the algorithm step-by-step:

  1. Use two pointers, slow and fast, to find the middle of the list. The slow pointer moves one step at a time, while the fast pointer moves two steps at a time.
  2. Reverse the second half of the list starting from the slow pointer.
  3. Compare the values in the first half and the reversed second half.
  4. Restore the original list structure by reversing the second half again (optional).

Code Implementation

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

def isPalindrome(head: ListNode) -> bool:
    # Step 1: Find the middle of the linked list
    slow, fast = head, head
    while fast and fast.next:
        slow = slow.next
        fast = fast.next.next

    # Step 2: Reverse the second half of the linked list
    prev = None
    while slow:
        next_node = slow.next
        slow.next = prev
        prev = slow
        slow = next_node

    # Step 3: Compare the first half and the reversed second half
    left, right = head, prev
    while right:  # Only need to compare till the end of the shorter half
        if left.val != right.val:
            return False
        left = left.next
        right = right.next

    return True

Complexity Analysis

The time complexity of this approach is O(n) because we traverse the list a constant number of times. The space complexity is O(1) because we only use a few pointers and do not allocate extra space proportional to the input size.

Edge Cases

Consider the following edge cases:

Testing

To test the solution comprehensively, consider the following test cases:

# Test case 1: Palindrome list
head = ListNode(1, ListNode(2, ListNode(2, ListNode(1))))
assert isPalindrome(head) == True

# Test case 2: Non-palindrome list
head = ListNode(1, ListNode(2))
assert isPalindrome(head) == False

# Test case 3: Single node list
head = ListNode(1)
assert isPalindrome(head) == True

# Test case 4: Empty list
head = None
assert isPalindrome(head) == True

# Test case 5: List with all identical elements
head = ListNode(1, ListNode(1, ListNode(1, ListNode(1))))
assert isPalindrome(head) == True

Thinking and Problem-Solving Tips

When approaching such problems, consider the following tips:

Conclusion

In this blog post, we discussed how to determine if a singly linked list is a palindrome using an efficient algorithm with O(n) time and O(1) space complexity. We covered the problem definition, approach, algorithm, code implementation, complexity analysis, edge cases, and testing. Understanding and solving such problems is crucial for improving your algorithmic thinking and coding skills.

Additional Resources

For further reading and practice, consider the following resources: