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

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

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <stack>
  6. #include <string>
  7. #include <fstream>
  8. #include <map>
  9. #include <set>
  10. using namespace std;
  11.  
  12. struct node {
  13. int data;
  14. node *next;
  15. node() : data(), next(NULL) { }
  16. node(int d) : data(d), next(NULL) { }
  17. };
  18.  
  19. void push(node* &head, int k) {
  20. node *new_node = new node(k);
  21. new_node->next = head;
  22. head = new_node;
  23. }
  24.  
  25. void print(node* head) {
  26. while (head) {
  27. cout << head->data << " ";
  28. head = head->next;
  29. }
  30. cout << endl;
  31. }
  32.  
  33. void reverselist(node *&head) {
  34. if (!head) return;
  35. node *cur = head;
  36. node *next = head->next;
  37. if (!next) return;
  38. reverselist(next);
  39. cur->next->next = cur;
  40. cur->next = NULL;
  41. head = next;
  42. }
  43.  
  44. bool compare(node *first, node* second) {
  45. while (first && second) {
  46. if (first->data != second->data) return false;
  47. first = first->next;
  48. second = second->next;
  49. }
  50. return first == NULL && second == NULL;
  51. }
  52.  
  53. bool palin(node *head) {
  54. if (!head || !head->next) return true;
  55. node *p, *q, *pre;
  56. p = q = pre = head;
  57. node *midnode = NULL;
  58. while (q && q->next) {
  59. q = q->next->next;
  60. pre = p;
  61. p = p->next;
  62. }
  63. if (q) { //odd number
  64. midnode = p;
  65. p = p->next;
  66. }
  67. node *second = p;
  68. pre->next = NULL;
  69. reverselist(second);
  70. bool ans = compare(head, second);
  71. reverselist(second);
  72. if (midnode) {
  73. pre->next = midnode;
  74. midnode->next = second;
  75. }
  76. else pre->next = second;
  77. return ans;
  78. }
  79.  
  80. int main() {
  81. node *head = NULL;
  82. push(head, );
  83. push(head, );
  84. push(head, );
  85. push(head, );
  86. push(head, );
  87. push(head, );
  88. if (palin(head)) cout << "yes" << endl;
  89. else cout << "no" << endl;
  90. print(head);
  91. return ;
  92. }

recursive的法子很巧

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <queue>
  5. #include <stack>
  6. #include <string>
  7. #include <fstream>
  8. #include <map>
  9. #include <set>
  10. using namespace std;
  11.  
  12. struct node {
  13. int data;
  14. node *next;
  15. node() : data(), next(NULL) { }
  16. node(int d) : data(d), next(NULL) { }
  17. };
  18.  
  19. void push(node* &head, int k) {
  20. node *new_node = new node(k);
  21. new_node->next = head;
  22. head = new_node;
  23. }
  24.  
  25. void print(node* head) {
  26. while (head) {
  27. cout << head->data << " ";
  28. head = head->next;
  29. }
  30. cout << endl;
  31. }
  32.  
  33. bool palinhelp(node *&left, node *right) {
  34. if (right == NULL) return true;
  35. bool palin1 = palinhelp(left, right->next);
  36. if (!palin1) return false;
  37. bool palin2 = right->data == left->data;
  38. left = left->next;
  39. return palin2;
  40. }
  41.  
  42. bool palin(node *head) {
  43. return palinhelp(head, head);
  44. }
  45.  
  46. int main() {
  47. node *head = NULL;
  48. push(head, );
  49. push(head, );
  50. push(head, );
  51. push(head, );
  52. push(head, );
  53. push(head, );
  54. if (palin(head)) cout << "yes" << endl;
  55. else cout << "no" << endl;
  56. print(head);
  57. return ;
  58. }

Data Structure Linked List: Function to check if a singly linked list is palindrome的更多相关文章

  1. [cc150] check palindrome of a singly linked list

    Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse ...

  2. [转]Data Structure Recovery using PIN and PyGraphviz

    Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...

  3. Summary: Trie Data Structure

    Implement a Trie Data Structure, and search() & insert() function: we need to implement both Cla ...

  4. [Algorithm] Heap data structure and heap sort algorithm

    Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...

  5. [LeetCode] All O`one Data Structure 全O(1)的数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  6. Leetcode: All O`one Data Structure

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

  7. [UVA] 11995 - I Can Guess the Data Structure! [STL应用]

    11995 - I Can Guess the Data Structure! Time limit: 1.000 seconds Problem I I Can Guess the Data Str ...

  8. Add and Search Word - Data structure design 解答

    Question Design a data structure that supports the following two operations: void addWord(word) bool ...

  9. [leetcode]432. All O`one Data Structure全O(1)数据结构

    Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...

随机推荐

  1. Java-帮助文档的制作

    Java-帮助文档的制作 1,public修饰的类才干够用bin/javadoc生成文档 2.java的说明书是通过文档的凝视来完毕的,所以在敲代码的时候.凝视是非常有必要的 使用文档凝视法,才干够生 ...

  2. UIWebView 加载网页、文件、 html

    UIWebView  是用来加载加载网页数据的一个框.UIWebView可以用来加载pdf word doc 等等文件 生成webview 有两种方法,1.通过storyboard 拖拽 2.通过al ...

  3. js从$scope外部调用$scope内部函数,跨js调用非全局函数

    scope内部函数定义 //定位 $scope.LocateByPoint = function (x,y) { if(!x || !y) { window.alert("GPS坐标不存在, ...

  4. cache和buffer区别探讨

    一. 1.Buffer(缓冲区)是系统两端处理速度平衡(从长时间尺度上看)时使用的.它的引入是为了减小短期内突发I/O的影响,起到流量整形的作用.比如生产者——消费者问题,他们产生和消耗资源的速度大体 ...

  5. eclipse 创建maven web错误Cannot change version of project facet Dynamic web module to 3.1解决方案

    Dynamic Web Module 选择“3.1”,java选择“1.8”,报错:Cannot change version of project facet Dynamic web module ...

  6. Linux - vim安装 配置与使用

    一 Vim 简单介绍 曾经一直用vi,近期開始使用 vim,以下将两者做一下比較. vi和vim都是word=%E5%A4%9A%E6%A8%A1&fr=qb_search_exp&i ...

  7. C# 为枚举创建新方法

    可以使用扩展方法添加特定于某个特定枚举类型的功能. 示例在下面的示例中,Grades 枚举表示学生可能在班里收到的字母等级分.该示例将一个名为 Passing 的扩展方法添加到 Grades 类型中, ...

  8. RealProxy AOP过滤方法的参数

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  9. 【数据挖掘】分类之Naïve Bayes(转载)

    [数据挖掘]分类之Naïve Bayes 1.算法简介 朴素贝叶斯(Naive Bayes)是监督学习的一种常用算法,易于实现,没有迭代,并有坚实的数学理论(即贝叶斯定理)作为支撑. 本文以拼写检查作 ...

  10. Android Studio 使用笔记:文件查询方法总结

    搜索单词 Windows: Ctrl + F Mac   : Cmd + F 会在当前激活的文件上查询输入的关键字,以高亮显示 跳转行 Windows: Ctrl + L Mac   : Cmd + ...