FastPrepLinked List Palindrome in Constant Space

Linked List Palindrome in Constant Space

Meta logoMeta● EasyFULLTIMEPHONE SCREEN

Updated

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.

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
head
1221

This list is an even-length palindrome.

Example 2

head = [1,2]return = false
head
12

This list is not a palindrome.

Example 3

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

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 Phone Screen questions

Similar questions at other companies

Meta interview experiences

All Meta interview questionsSee Meta hiring insights
Saved
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public boolean isPalindrome(ListNode head) {
        // Write your code here
    }
}
Checking account…