[LeetCode] 234. Palindrome Linked List
来源:互联网 发布:临沂php培训 编辑:程序博客网 时间:2024/06/08 14:31
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?
class Solution {public: bool isPalindrome(ListNode* head) { ListNode *FstHalf = head; ListNode *SndHalf = HalfHead(head); SndHalf = Reverse(SndHalf); while (FstHalf && SndHalf) { if (FstHalf->val != SndHalf->val) return false; FstHalf = FstHalf->next; SndHalf = SndHalf->next; } return true; }private: ListNode *HalfHead(ListNode *head) { ListNode *slow, *fast; slow = fast = head; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } return fast ? slow->next : slow; } ListNode *Reverse(ListNode *head) { if (head == nullptr || head->next == nullptr) return head; ListNode *revhead = Reverse(head->next); head->next->next = head; head->next = nullptr; return revhead; } /* ListNode *Reverse(ListNode *head) { ListNode *prev = nullptr, *cur; while (head) { cur = head; head = head->next; cur->next = prev; prev = cur; } return prev; } */};
阅读全文
0 0
- [leetcode] 234.Palindrome Linked List
- [LeetCode]234.Palindrome Linked List
- [leetcode] 234.Palindrome Linked List
- 【leetcode】234. Palindrome Linked List
- 【LeetCode】234. Palindrome Linked List
- [leetcode] 234. Palindrome Linked List
- 234. Palindrome Linked List LeetCode
- LeetCode--234. Palindrome Linked List
- LeetCode 234. Palindrome Linked List
- 【LeetCode】234. Palindrome Linked List
- leetcode 234. Palindrome Linked List
- LeetCode *** 234. Palindrome Linked List
- LeetCode 234. Palindrome Linked List
- leetcode-234. Palindrome Linked List
- LeetCode-234.Palindrome Linked List
- 【LeetCode】234. Palindrome Linked List
- [Leetcode]234. Palindrome Linked List
- 【leetcode】234. Palindrome Linked List
- 剑指offer:数字在排序数组中的次数
- 3.matlab语句的基本结构
- 【HTML】- 回到顶部
- 海量数据处理
- Matlab中max()函数的用法
- [LeetCode] 234. Palindrome Linked List
- 第7章 Spring MVC的高级技术--图片上传基于StandardServletMultipartResolver
- LeetCode之Find All Duplicates in an Array
- Python 实现LRU Cache
- Linux基础—>和>>的区别,<号的使用
- 网络请求的一些步骤和操作
- 注册表读取CPU信息
- Saliency Detection by Multi-Task Sparsity Puisuit
- html标签使用注意事项