FastPrepLinked List Palindrome in Constant Space

Linked List Palindrome in Constant Space

Meta logoMeta● EasyFULLTIMEPHONE SCREEN
Learn

Problem statement

Given the head of a singly linked list, return true when its values read the same from left to right and right to left; otherwise return false.

Meet the reported follow-up: use O(n) time and O(1) extra space, where n is the number of nodes.

Function

isPalindrome(head: ListNode) → boolean

Examples

Example 1

head = [1,2,2,1]return = true

The linked task reference gives this even-length palindrome.

Example 2

head = [1,2]return = false

The linked task reference gives this non-palindrome.

Example 3

head = [1,2,3,2,1]return = true

An odd middle node is unpaired; the remaining symmetric values match.

Constraints

  • The list is acyclic and contains between 1 and 10^5 nodes.
  • 0 <= Node.val <= 9.

More Meta problems

See Meta hiring insights
public boolean isPalindrome(ListNode head) {
    // Write your code here
}
head[1,2,2,1]
expectedtrue
Checking account…