leetcode 234 Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.
Follow up:
Could you do it in O(n) time and O(1) space?
- using two pointers:
- A
slowpointer with step length equals One - A
fastpointer with step length eauqls Two
- A
- We can get the middle elements pointed by slow when the total number of the array is odd.
Slowpointer points at the larger one when the array is at a length of even.- We can implements an function which reverses the linked list, So we can get the reverse list over [slow, -1].
- compare
headwithslowstep by step untilslowpoints to null.
1 | /** |