Problem
Follow up
Could you do it in O(n) time and O(1) space?
Java
Break and reverse second half
© LeetCode – Palindrome Linked List (Java) - Java Solution 2 - Break and reverse second half
We can use a fast and slow pointer to get the center of the list, then reverse the second list and compare two sublists. The time is O(n) and space is O(1).
1 | /** |
Submission Detail
- 26 / 26 test cases passed.
- Runtime: 1 ms, faster than 95.13% of Java online submissions for Palindrome Linked List.
- Memory Usage: 41.7 MB, less than 5.11% of Java online submissions for Palindrome Linked List.
extrap space
1 | /** |
Submission Detail
- 26 / 26 test cases passed.
- Runtime: 2 ms, faster than 39.06% of Java online submissions for Palindrome Linked List.
- Memory Usage: 42.8 MB, less than 5.11% of Java online submissions for Palindrome Linked List.
Recursion
1 | /** |
Submission Detail
- 26 / 26 test cases passed.
- Runtime: 1406 ms, faster than 5.02% of Java online submissions for Palindrome Linked List.
- Memory Usage: 42.7 MB, less than 5.11% of Java online submissions for Palindrome Linked List.