Skip to the code
cf.
+10 XP

#232 Symmetric Playlist Check

easy

A music app has a "mirror mix" badge for playlists that read the same forwards and backwards, such as track ids 4, 9, 9, 4. The playlist is a linked list of track ids.

Write isMirrorMix(head) that returns true if the values read the same in both directions, otherwise false. An empty playlist counts as a mirror mix. Try to use only O(1) extra space.

Example 1

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

Read backwards it is still 1, 2, 2, 1.

Example 2

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

Backwards it is 2, 1, which is different.

Constraints

  • The list has 0 to 10^5 nodes.
  • 0 <= node.val <= 9

Lists are written as arrays here, like [1, 2, 3]. Your function works with ListNode objects, each with val and next.

Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).