Linked Lists Fast and slow pointers
291. Palindrome Linked List
You are given head, the first node of a singly linked list. Return true if the list is a palindrome, and
false otherwise.
A list is a palindrome when its values read the same from the head to the last node as from the last node back to the head. Each node holds a whole number, and two values match only when they are the same number.
The examples write a list as its values from the head on, so head = [4,1,6] is the list 4 → 1 → 6.
Example 1
Input: head = [3, 7, 0, 7, 3]
trueExplanation: Read from the last node back to the head, the values are 3, 7, 0, 7, 3, the same as from the head on. The middle value 0 has no partner, and it does not need one.
Example 2
Input: head = [40, -9, 9, 40]
falseExplanation: The first and last values are both 40, but the second value is -9 and the second from the end is 9. Those are different numbers, so the list is not a palindrome.
Example 3
Input: head = [12, 21]
falseExplanation: Backwards the list is 21 → 12, which is a different list. The digits 1, 2, 2, 1 read the same both ways, but the list holds two numbers, 12 and 21, not four digits.
Check your understanding
Work out the answer for this input before you write any code.
Constraints
1 ≤ number of nodes ≤ 105-109 ≤ Node.val ≤ 109
Clarifying questions
What you would ask an interviewer before writing any code.