BM13. 判断一个链表是否为回文结构
描述
示例1
输入:
{1}
输出:
true
示例2
输入:
{2,1}
输出:
false
说明:
2->1示例3
输入:
{1,2,2,1}
输出:
true
说明:
1->2->2->1C++ 解法, 执行用时: 13ms, 内存消耗: 5748KB, 提交时间: 2021-09-12
// * // * struct ListNode { // * int val; // * struct ListNode *next; // * }; static int x = []{ std::ios::sync_with_stdio(false); cin.tie(NULL); return 0; }(); class Solution { public: /** * * @param head ListNode类 the head * @return bool布尔型 */ bool isPail(ListNode* head) { // write code here ListNode* slow = head; ListNode* fast = head; if(!fast->next) return true; while(fast&&fast->next) { slow=slow->next; fast=fast->next->next; } if(fast) slow= slow->next; ListNode* tmp = reverse(slow); while(head && tmp) { if(head->val != tmp->val) return false; head=head->next; tmp=tmp->next; } return true; } ListNode* reverse(ListNode* root) { if(!root) return root; ListNode* res = nullptr; ListNode* pre = root; while(root) { pre = root; root=root->next; pre->next = res; res=pre; } return res; } };
C++ 解法, 执行用时: 13ms, 内存消耗: 6196KB, 提交时间: 2021-11-11
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ static int x = []{ std::ios::sync_with_stdio(false); cin.tie(NULL); return 0; }(); class Solution { public: /** * * @param head ListNode类 the head * @return bool布尔型 */ bool isPail(ListNode* head) { // write code here if(!head || !head->next) return true; vector<int> temp; while(head) { temp.push_back(head->val); head = head->next; } int j = temp.size()-1; int i = 0; while(i < j){ if(temp[i++] != temp[j--]) return false; } return true; } };
C++ 解法, 执行用时: 14ms, 内存消耗: 5720KB, 提交时间: 2021-08-04
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ static int x = []{ std::ios::sync_with_stdio(false); cin.tie(NULL); return 0; }(); class Solution { public: /** * * @param head ListNode类 the head * @return bool布尔型 */ bool isPail(ListNode* head) { // write code here if(!head || !head->next) return true; ListNode *fast = head, *slow = head; while(fast && fast->next) { fast = fast->next->next; slow = slow->next; } fast = slow->next; slow->next = nullptr; ListNode *newList = nullptr; while(fast) { newList = fast->next; fast->next = slow; slow = fast; fast = newList; } newList = slow; fast = head; while(fast && newList) { if(fast->val != newList->val) return false; fast = fast->next; newList = newList->next; } return true; } };
C++ 解法, 执行用时: 14ms, 内存消耗: 6272KB, 提交时间: 2021-06-22
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 the head * @return bool布尔型 */ bool isPail(ListNode* head) { // write code here if(head == nullptr || head->next == nullptr) { return true; } vector<int> tmp; while(head) { tmp.push_back(head->val); head = head->next; } int left = 0; int right = tmp.size() - 1; while(left < right) { if(tmp[left] != tmp[right]) { return false; } left++; right--; } return true; } }; static const auto io_sync_off = []()//lambda函数 { // turn off sync,关闭输入输出流的缓存 std::ios::sync_with_stdio(false); // untie in/out streams,实现输入和输出流的解绑 std::cin.tie(nullptr); return nullptr; }();
C++ 解法, 执行用时: 15ms, 内存消耗: 5652KB, 提交时间: 2021-08-30
/** * struct ListNode { * int val; * struct ListNode *next; * }; */ static int x = []{ std::ios::sync_with_stdio(false); cin.tie(NULL); return 0; }(); class Solution { public: /** * * @param head ListNode类 the head * @return bool布尔型 */ bool isPail(ListNode* head) { // write code here if(!head || !head->next) return true; ListNode *fast = head, *slow = head; while(fast && fast->next) { fast = fast->next->next; slow = slow->next; } fast = slow->next; slow->next = nullptr; ListNode *newList = nullptr; while(fast) { newList = fast->next; fast->next = slow; slow = fast; fast = newList; } newList = slow; fast = head; while(fast && newList) { if(fast->val != newList->val) return false; fast = fast->next; newList = newList->next; } return true; } };