Problem 278966 · medium · Phase 02 Linear Data Structures

Mirror-Image List

linked list · reversal · two pointers

Given the head of a singly linked list, return True if the sequence of values reads the same forwards and backwards, and False otherwise. An empty list counts as a mirror image.

Examples

Input:  head = 1 -> 2 -> 2 -> 1
Output: True

Input:  head = 1 -> 2 -> 3 -> 2
Output: False

Constraints

  • 0 <= number of nodes <= 10**4
  • Target: O(n) time and O(1) extra space, without copying the values into a Python list

Goals

  • Combine finding the middle with reversing a half
  • Compare two halves node by node
Starting Python…