It's the Xinrui Ma

Blog

Given a singly linked list, determine if it is a palindrome.

Posted by in LeetCode on

Given a singly linked list, determine if it is a palindrome.

Example 1:

Input: 1->2
Output: false

Example 2:

Input: 1->2->2->1
Output: true

Follow up:
Could you do it in O(n) time and O(1) space?

Solution:
1.get the length of the link list.
2. get the second half of the linked list
3. reverse the second half of linked list
4. compare beginning of list and the reversed second half, until reversed equals null