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 ;
}

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. CoreImage的模糊滤镜

    //1.原始图片 UIImage * image = [UIImage imageNamed:@"1.jpg"]; /****************core image***** ...

  2. Android XMPP 即时通讯

    0 http://blog.csdn.net/lnb333666/article/details/7471292 0.1 http://www.cnblogs.com/charley_yang/arc ...

  3. Android应用架构之MVP---&gt;天气实例

    我们知道.Android App 本质上抽象成两个层次:视图和数据.为了App在发展过程中高速的适应变化,方便维护和高速迭代,我们要将数据和视图解耦,而在解藕方面我们的前辈们在漫长的软件开发经验中为我 ...

  4. eventfd

    #include <sys/eventfd.h> int eventfd(unsigned int initval, int flags); eventfd() creates an &q ...

  5. [译]GLUT教程 - 子菜单

    Lighthouse3d.com >> GLUT Tutorial >> Pop-up Menus >> Sub Menus 上一节我们介绍了如何创建普通菜单和如果 ...

  6. simple_pool对象池——优化&lt;二&gt;

    本文章由cartzhang编写.转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/details/55051570 作者:car ...

  7. LeetCode78:Subsets

    Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset must ...

  8. mac Xvim 语法高亮

    步骤1: cp /usr/share/vim/vimrc ~/.vimrc 先复制一份vim配置模板到个人目录下 注:redhat 改成 cp /etc/vimrc ~/.vimrc 步骤2: vi ...

  9. spring+mybatis基于 AOP实现业务日志管理

    最近在项目上用到了操作日志的相关,之前的解决方案就是自己写一个日志project,然后统一调用日志接口即可,这样方便自定义定制,因为有很多设备控制之类的都是需要确认一下的,但是,对数据的操作,比如,增 ...

  10. java程序优化

    程序代码优化要点: 字符串优化:分析String源码,了解String常用方法,使用StringBuffer.StringBuilder. List.Map.Set优化:分析常用ArrayList.L ...