Skills101
← All coding problems

Palindrome Linked List

easyLinked List+10 XPTime limit 2s

Does the linked list read the same from head to tail as from tail to head?

List format. A linked list is given as two lines: its length n, then its n values from head to tail (the second line is empty when n = 0). When you print a list, print its values on one line separated by spaces, or the word empty if it has no nodes.

Input: one list (n ≥ 1).

Output: true or false.

Constraints: 1 ≤ n ≤ 10⁵, 0 ≤ value ≤ 9.

Challenge: copying the values into an array is easy. The interview version wants O(1) extra memory: find the middle (slow/fast pointers), reverse the second half, compare the halves — and restore the list afterwards.

Sample input 1

4
1 2 2 1

Sample output 1

true

Sample input 2

2
1 2

Sample output 2

false

📖 Stuck? Learn the pattern

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.