Palindrome Linked List of Leetcode

Topic description

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?

Problem solving ideas

1. Traverse the entire linked list, record the value of each node of the linked list in the array, and then judge whether the array is a palindrome array. The time complexity is O(n), but the space complexity is also O(n), which is not satisfied Space complexity requirements.

2. Using the first-in-last-out property of the stack, push the first half of the linked list into the stack, and then pop it out one by one to compare with the second half of the linked list. The time complexity is O(n), but it still requires n/2 stack space, and the space complexity is O(n).

3. Reverse the linked list method, flip the second half of the linked list in place, and then compare the first half and the second half in turn to judge whether they are equal. The time complexity is O(n), and the space complexity is O(1) to meet the requirements of the question.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325599999&siteId=291194637