http://www.geeksforgeeks.org/function-to-check-if-a-singly-linked-list-is-palindrome/

这里的reverse可以reverse整个list,这样空间需求就是O(n),不如这个网页写的O(1)的方法

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
#include <set>
using namespace std; struct node {
int data;
node *next;
node() : data(), next(NULL) { }
node(int d) : data(d), next(NULL) { }
}; void push(node* &head, int k) {
node *new_node = new node(k);
new_node->next = head;
head = new_node;
} void print(node* head) {
while (head) {
cout << head->data << " ";
head = head->next;
}
cout << endl;
} void reverselist(node *&head) {
if (!head) return;
node *cur = head;
node *next = head->next;
if (!next) return;
reverselist(next);
cur->next->next = cur;
cur->next = NULL;
head = next;
} bool compare(node *first, node* second) {
while (first && second) {
if (first->data != second->data) return false;
first = first->next;
second = second->next;
}
return first == NULL && second == NULL;
} bool palin(node *head) {
if (!head || !head->next) return true;
node *p, *q, *pre;
p = q = pre = head;
node *midnode = NULL;
while (q && q->next) {
q = q->next->next;
pre = p;
p = p->next;
}
if (q) { //odd number
midnode = p;
p = p->next;
}
node *second = p;
pre->next = NULL;
reverselist(second);
bool ans = compare(head, second);
reverselist(second);
if (midnode) {
pre->next = midnode;
midnode->next = second;
}
else pre->next = second;
return ans;
} int main() {
node *head = NULL;
push(head, );
push(head, );
push(head, );
push(head, );
push(head, );
push(head, );
if (palin(head)) cout << "yes" << endl;
else cout << "no" << endl;
print(head);
return ;
}

recursive的法子很巧

 #include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
#include <set>
using namespace std; struct node {
int data;
node *next;
node() : data(), next(NULL) { }
node(int d) : data(d), next(NULL) { }
}; void push(node* &head, int k) {
node *new_node = new node(k);
new_node->next = head;
head = new_node;
} void print(node* head) {
while (head) {
cout << head->data << " ";
head = head->next;
}
cout << endl;
} bool palinhelp(node *&left, node *right) {
if (right == NULL) return true;
bool palin1 = palinhelp(left, right->next);
if (!palin1) return false;
bool palin2 = right->data == left->data;
left = left->next;
return palin2;
} bool palin(node *head) {
return palinhelp(head, head);
} int main() {
node *head = NULL;
push(head, );
push(head, );
push(head, );
push(head, );
push(head, );
push(head, );
if (palin(head)) cout << "yes" << endl;
else cout << "no" << endl;
print(head);
return ;
}

最新文章

  1. 算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!
  2. Redis设置认证密码 Redis使用认证密码登录 在Redis集群中使用认证密码
  3. PHP小总结
  4. yum常用命令
  5. 如何让Notepad++添加Python运行方式.精讲
  6. 进程控制块(Process Control Block, PCB)
  7. UML用例图中泛化、扩展、包括
  8. 点餐APP 冲刺三总结
  9. POJ 2065 SETI (高斯消元 取模)
  10. C# Json数据反序列化为Dictionary并根据关键字获取指定值1
  11. APP的测试过程和重点
  12. STM8S学习笔记-时钟控制2
  13. ActionBar +Tab+ViewPager +Fragment 支持侧滑动完成办税工具的页面展示
  14. mysql 简单游标
  15. php反射类 ReflectionClass
  16. javascript之事件处理
  17. (NO.00005)iOS实现炸弹人游戏(八):游戏主角(一)
  18. 13.Git分支-变基(rebase)、rebase VS merge
  19. Masonry 与 frame 混用导致的问题
  20. Web概述

热门文章

  1. jquery 插件:chosen
  2. PHP curl post header
  3. Linux系统控制文件 /etc/sysctl.conf详解
  4. GitHub for window 使用教程
  5. mac eclipse 删除不用的workspace
  6. hibernate实现多变联合查询
  7. Create React App
  8. 【Mac系统】之fiddler下载和安装
  9. bugzilla部署记录
  10. PHP-Manual的学习----【语言参考】----【类型】-----【NULL】