234. Palindrome Linked List
problem description
Given a singly linked list, determine if it is a palindrome.
Example 1:
Example 2:
Follow up: Could you do it in O(n) time and O(1) space?
algorithm thought
这里主要是找到链表的中点,将链表分成两半,然后匹配是否是回文串。找到中点用快慢指针。使用一个stack来辅助,就只需要一次遍历可的结果
code
algorithm analysis
一次遍历,时间复杂度是O(n),使用栈辅助,空间复杂度也是O(n)
Last updated