302. Chain Reads Both Ways
A bead necklace has been cut open into a chain: each node holds the number engraved on one bead. A jeweller says the arrangement is mirror-perfect when reading the numbers from the first bead to the last gives exactly the same sequence as reading them from the last bead to the first.
Given the head of the chain, return true if it is mirror-perfect and false otherwise. An empty chain and a single bead both count as mirror-perfect. You cannot jump to an arbitrary position, because each node only knows the one after it. The chain holds up to 100,000 nodes.
Example 1
- Input:
- head = [12,-3,40,40,-3,12]
- Output:
- true
- Explanation:
Reading backwards gives 12, -3, 40, 40, -3, 12, which is identical.
Example 2
- Input:
- head = [5,9,5,9]
- Output:
- false
- Explanation:
Backwards it reads 9, 5, 9, 5, which differs from the forward order.
Constraints
0 ≤ chain length ≤ 105
-1000 ≤ node value ≤ 1000
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n)
- Space
- O(1)