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. BZOJ 1012 线段树||单调队列

    非常裸的线段树  || 单调队列: 假设一个节点在队列中既没有时间优势(早点入队)也没有值优势(值更大),那么显然不管在如何的情况下都不会被选为最大值. 既然它仅仅在末尾选.那么自然能够满足以上的条件 ...

  2. BigDecimal舍入模式使用及建议

    1. 八种舍入模式 此节内容参考于 https://my.oschina.net/sunchp/blog/670909. JDK1.5发布的枚举 RoundingMode 对 BigDecimal 的 ...

  3. iOS项目工程添加.a文件遇到的Dsymutil Error

    将.a文件加入工程,很多教程讲的都是: 右键选择Add->Existing Files…,选择.a文件和相应的.h头文件.或者将这两个文件拖入XCode工程目录结构中,在弹出的界面中勾选Copy ...

  4. 2.JAVA编程思想——一切都是对象

    一切都是对象 欢迎转载.转载请标明出处:http://blog.csdn.net/notbaron/article/details/51040221 虽然以C++为基础,但 Java 是一种更纯粹的面 ...

  5. Android---06---2中动画效果

    1,先看FrameAnimation,此动画是用来实现动态动画就是把一帧帧图片迭代起来 放在drowable中的xml: <?xml version="1.0" encodi ...

  6. 高盛CEO致大学毕业生:要与有野心的人为伍

    我认为讲的非常棒.年轻人就要这样. 高盛集团首席运行官(CEO)劳尔德-贝兰克梵(Lloyd Blankfein)周四在曼哈顿贾维茨中心參加了拉瓜迪亚社区大学的第41届毕业典礼并发表演讲.在面向约10 ...

  7. ArrayList add remove 代码分析

    Add 首次add 元素需要对数组进行扩容(初始化Capacity 10, 第二次扩容10>>1 为5, 第三次扩容15>>1 为7), 每次扩容之前长度的1.5倍,当add ...

  8. linux head-common.s分析(转)

    供head.S调用,其中__mmap_switched的b start_kernel跳转到C执行,且永不返回. 跳转到start_kernel时寄存器值: R0 = cp#15 control reg ...

  9. Android Java 线程池 ThreadPoolExecutor源代码篇

    线程池简单点就是任务队列+线程组成的. 接下来我们来简单的了解下ThreadPoolExecutor的源代码. 先看ThreadPoolExecutor的简单类图,对ThreadPoolExecuto ...

  10. 【ubantu】Ubuntu的一些常用命令

    创建文件: touch a.txt 创建文件夹: mkdir NewFolder 删除文件: rm a.txt 删除文件夹: rmdir NewFolder 删除带有文件的文件夹: rm -rf Ne ...