Palindrome Linked List
EasyP2Coding
Given the head of a singly linked list, return true if its values read the same from front to back and from back to front, otherwise false. A list node has val and next. The ListNode class is already defined for you.
Aim for O(n) time and O(1) extra space.
Example 1
- Input
- head = [1,2,2,1]
- Output
- true
Example 2
- Input
- head = [1,2,3]
- Output
- false
Example 3
- Input
- head = [7]
- Output
- true
Constraints
- 0 <= number of nodes <= 10^5
- 0 <= Node.val <= 9
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 12. ⌘Enter runs, ⌘⇧Enter submits.