All problems

Problem 117

Palindrome Linked List

Easy · 10 pts · Linked List, Two Pointers, Stack

Problem

You are given a linked list of digits. Decide whether its values read the same from head to tail as from tail to head. Copying the values into an array and comparing from both ends is the simple way. The O(1)-memory approach finds the middle with slow and fast pointers, reverses the second half, and then compares the first half with the reversed second half node by node.

Input

First line: integer N, the number of nodes. Second line: the N node values from head to tail.

Output

Print YES or NO.

Constraints

1 <= N <= 100000; 0 <= value <= 9

Example input

4
1 2 2 1

Example output

YES

Explanation

The list 1 2 2 1 read backwards is again 1 2 2 1, so it is a palindrome: YES.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Partition List Next: Remove Duplicates from Sorted List