FastPrepBuild and Check a Palindrome Linked List
Problem · Linked List

Build and Check a Palindrome Linked List

Learn this problem
EasyOmnissa logoOmnissaFULLTIMEONSITE INTERVIEW

Problem statement

You are given an integer array values listing node values from head to tail. Define your own singly linked-list node structure and build the corresponding list. Return true if the list reads the same forward and backward, or false otherwise.

The array is only an input adapter. Practice the list algorithm: locate the middle, reverse the second half, and compare corresponding node values. Once the list has been built, aim to use constant extra space for the comparison. The empty list and a one-node list are palindromes.

The judge checks the returned Boolean; it does not inspect the internal node implementation. The editorial demonstrates the required construction and pointer operations.

Function

isPalindrome(values: int[]) → boolean

Examples

Example 1

values = [3,5,5,3]return = true

The list reads 3,5,5,3 in either direction. Reversing its second half aligns [3,5] with the first half.

Example 2

values = [3,5,7]return = false

The head and tail values, 3 and 7, do not match.

Constraints

  • 0 <= values.length <= 20000
  • -1000000000 <= values[i] <= 1000000000
  • The constructed list is singly linked and contains no cycle.

More Omnissa problems

drafts saved locally
public boolean isPalindrome(int[] values) {
    // Define nodes, build the list, and compare its halves.
}
values[3,5,5,3]
expectedtrue
checking account