Linked List Palindrome in Constant Space
Problem statement
Given the head of a singly linked list, return true when its values read the same from left to right and right to left; otherwise return false.
Meet the reported follow-up: use O(n) time and O(1) extra space, where n is the number of nodes.
Function
isPalindrome(head: ListNode) → booleanExamples
Example 1
head = [1,2,2,1]return = trueThe linked task reference gives this even-length palindrome.
Example 2
head = [1,2]return = falseThe linked task reference gives this non-palindrome.
Example 3
head = [1,2,3,2,1]return = trueAn odd middle node is unpaired; the remaining symmetric values match.
Constraints
- The list is acyclic and contains between
1and10^5nodes. 0 <= Node.val <= 9.