↓Skip to main content
  1. LeetCode/

LeetCode 234: Palindrome Linked List

·2 mins· ·
LeetCode Easy Linked-List Two-Pointers Stack Recursion
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-22

  • Submissions: Runtime Error → Time Limit Exceeded → Accepted ×2

Approach
#

A list cannot be walked backward, so reverse the second half (Linked List Templates 2 and 3):

  1. Find the middle with fast and slow pointers.
  2. Reverse from the middle on.
  3. Compare from the head and from the reversed half’s head together until the second half ends (with an odd length the middle needs no comparison).

My Attempts
#

Attempt 1 had a syntax error; attempt 2’s loop pointer never advanced, so [1] looped forever (TLE). My accepted version copied the values into a list; here it is the O(1)-space approach.

Solution
#

class Solution:
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        slow = fast = head
        while fast and fast.next:
            slow, fast = slow.next, fast.next.next
        prev = None
        while slow:                                  # 反轉後半段
            slow.next, prev, slow = prev, slow, slow.next
        while prev:
            if prev.val != head.val:
                return False
            prev, head = prev.next, head.next
        return True

Complexity
#

Time O(n), space O(1).

Mistakes to Avoid
#

  • Compare only until the second half ends; the first half’s tail still points at the middle node.
  • This modifies the list; if that is not allowed, copy the values into an array (O(n) space).

Interview Explanation
#

I find the middle with fast and slow pointers, reverse the second half, and compare from the head and the reversed head together; all equal means a palindrome. Time O(n), extra space O(1).

Related

LeetCode 203: Remove Linked List Elements
·2 mins
LeetCode Easy Linked-List Recursion
LeetCode 203 note: the head may be deleted too, so a dummy head turns every deletion into deleting cur.next; do not advance after deleting
LeetCode 206: Reverse Linked List
·1 min
LeetCode Easy Linked-List Recursion
LeetCode 206 note: three pointers prev / cur / nxt; save the next node, then point cur.next back at prev
LeetCode 141: Linked List Cycle
·2 mins
LeetCode Easy Hash-Table Linked-List Two-Pointers Floyds-Cycle-Finding-Algorithm
LeetCode 141 note: Floyd’s cycle detection, fast two steps and slow one; with a cycle they must meet, each advancing from its own position
LeetCode 160: Intersection of Two Linked Lists
·2 mins
LeetCode Easy Hash-Table Linked-List Two-Pointers
LeetCode 160 note: each pointer walks its own list then the other’s; both cover lenA + lenB steps and reach the intersection, or None, together
LeetCode 231: Power of Two
·2 mins
LeetCode Easy Math Bit-Manipulation Recursion
LeetCode 231 note: a power of two has exactly one set bit, and n & (n - 1) clears the lowest one, so the result is 0 exactly then; n must be positive
LeetCode 232: Implement Queue using Stacks
·1 min
LeetCode Easy Stack Design Queue
LeetCode 232 note: two stacks, where in receives pushes and out is refilled from in only when empty, amortized O(1)