The problem. Given the head of a singly linked list, return true if it is a palindrome — it reads the same forwards and backwards — and false otherwise.
Input: head = [1, 2, 2, 1]
Output: true
Input: head = [1, 2]
Output: falseWith an array you'd compare arr[i] with arr[n - 1 - i]. A singly linked list can only walk forwards, so reaching "the second-to-last node" is the hard part.
Free account
Sign up to read the rest of this lesson: 5 more sections, 2 drawings, a dry-run simulator and code in JavaScript, Python, Java and C++.
Still to come