easyLinked ListTwo Pointers 0 views

Palindrome Linked List

Check whether the values in a singly-linked list read the same forwards and backwards.

Given the head of a singly-linked list, determine whether the sequence of values it contains forms a palindrome -- that is, whether the list reads the same from front to back as it does from back to front.

Return true if the list is a palindrome, or false otherwise.

Example 1

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

Output: true

Example 2

Input: head = [1,2]

Output: false

Example 3

Input: head = [1]

Output: true

Explanation: A single node is trivially a palindrome.

Constraints

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

Follow-up

Can you solve it in O(n) time and O(1) space, without copying the values into another data structure?

Hints

Companies

No companies reported yet.

Discussion

Sign in to join the discussion.

Loading discussion...

Test results

No test cases yet.