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.
Members only · free account
The rest of this lesson, with 5 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson