234. Palindrome Linked List(链表模板)
2016-03-23 15:33
253 查看
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?
思路1:遍历一边链表,把它放到数组或VECTOR里,转化为字符串的回文判断。
但是时间复杂度和空间复杂度都是O(n)。
思路二:先找到链表的中间的那个点,把链表分割成2个部分。
这个方法可以用判断链表是否有环的思想,设置两个指针,
一个每次向前移动一步,一个每次移动二步。
然后将链表的一个部分反转,再依次进行比较。
还写了链表的测试代码。保留下,方便以后调试代码吧。
Follow up:
Could you do it in O(n) time and O(1) space?
思路1:遍历一边链表,把它放到数组或VECTOR里,转化为字符串的回文判断。
但是时间复杂度和空间复杂度都是O(n)。
class Solution { public: bool isPalindrome(ListNode* head) { vector<int> v; while(head){ v.push_back(head->val); head=head->next; } for(int i=0,j=v.size()-1;i<j;i++,j--){ if(v[i]!=v[j]) return false; } return true; } };
思路二:先找到链表的中间的那个点,把链表分割成2个部分。
这个方法可以用判断链表是否有环的思想,设置两个指针,
一个每次向前移动一步,一个每次移动二步。
然后将链表的一个部分反转,再依次进行比较。
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool isPalindrome(ListNode* head) { if(!head||!head->next){ return true; } ListNode *t1=head,*t2=head; while(t2&&t2->next){ t1=t1->next; t2=t2->next->next; } if(t2){ t1=t1->next; } t1=reverseList(t1); while(t1){ if(t1->val!=head->val) return false; t1=t1->next; head=head->next; } return true; } ListNode* reverseList(ListNode* head){ if(!head||!head->next)return head; ListNode *t1=head,*t2=head->next; t1->next=NULL; while(t2){ ListNode *t3=t2->next; t2->next=t1; t1=t2; t2=t3; } return t1;//错误点,return head; } };这个题目自己在写反转链表的时候,return的返回值写成了head让我花了好久才找出来了这个BUG。
还写了链表的测试代码。保留下,方便以后调试代码吧。
#include <iostream> #include<cstring> #include <vector> #include <cstdio> #include <assert.h> using namespace std; struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; ListNode *creatList(int n) { ListNode head(-1); ListNode *prev = &head; int val; for (int i = 0; i < n; ++i) { cin >> val ; prev->next = new ListNode(val); prev = prev->next; } prev->next = NULL; return head.next; } void printList(ListNode*pHead){ if(!pHead)return; while(pHead){ cout<<pHead->val<<" "; pHead=pHead->next; } cout<<endl; } ListNode* reverseList(ListNode* head){ if(!head||!head->next)return head; ListNode *t1=head,*t2=head->next; t1->next=NULL; while(t2){ ListNode *t3=t2->next; t2->next=t1; t1=t2; t2=t3; } return head; } bool isPalindrome(ListNode* head) { if(!head||!head->next){ return true; } ListNode *t1=head,*t2=head; while(t2&&t2->next){ t1=t1->next; t2=t2->next->next; } if(t2){ t1=t1->next; } t1=reverseList(t1); while(t1){ if(t1->val!=head->val) return false; t1=t1->next; head=head->next; } return true; } void destoryList(ListNode* pHead) { assert(pHead!=NULL); ListNode* pNext = pHead->next; while(pNext != NULL) { delete pHead; pHead = pNext; pNext = pHead->next; } delete pHead; pHead = NULL; return; } int main() { ListNode* head = creatList(4); printf("%d\n",isPalindrome(head)); destoryList(head); }
相关文章推荐
- mysql字符函数简析
- 最全的资源教程-前端涉及的所有知识体系
- Java实现QPS控制
- Oracle学习笔记【01】
- HDOJ 1754 I Hate It (线段树单点更新求区间最大值)
- 威胁情报平台
- 构建之法阅读笔记04
- Redis初学笔记
- 非常有用的101道算法部分常见面试题
- PCM data flow - part 6: Frames & Periods
- Android中 Handler使用全解
- 3747: [POI2015]Kinoman|线段树
- Beyond Compare详细功能介绍
- MySQL 字符串相关函数简析
- 绑定Service
- 怎样用QT在windows环境通过编程卸载installShield打包的程序
- 查询oracle约束所关联的表
- php调用阿里大鱼 接口curl
- Java [Leetcode 94]Binary Tree Inorder Traversal
- 为什么引用不了App_Code里的类(报“未能找到类型或命名空间名称”错误)