Data Structure Linked List: Function to check if a singly linked list is palindrome
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的更多相关文章
- [cc150] check palindrome of a singly linked list
Problem: Implement a function to check if a singly linked list is a palindrome. 思路: 最简单的方法是 Reverse ...
- [转]Data Structure Recovery using PIN and PyGraphviz
Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...
- Summary: Trie Data Structure
Implement a Trie Data Structure, and search() & insert() function: we need to implement both Cla ...
- [Algorithm] Heap data structure and heap sort algorithm
Source, git Heap is a data structure that can fundamentally change the performance of fairly common ...
- [LeetCode] All O`one Data Structure 全O(1)的数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- Leetcode: All O`one Data Structure
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- [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 ...
- Add and Search Word - Data structure design 解答
Question Design a data structure that supports the following two operations: void addWord(word) bool ...
- [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 ...
随机推荐
- Java-帮助文档的制作
Java-帮助文档的制作 1,public修饰的类才干够用bin/javadoc生成文档 2.java的说明书是通过文档的凝视来完毕的,所以在敲代码的时候.凝视是非常有必要的 使用文档凝视法,才干够生 ...
- UIWebView 加载网页、文件、 html
UIWebView 是用来加载加载网页数据的一个框.UIWebView可以用来加载pdf word doc 等等文件 生成webview 有两种方法,1.通过storyboard 拖拽 2.通过al ...
- js从$scope外部调用$scope内部函数,跨js调用非全局函数
scope内部函数定义 //定位 $scope.LocateByPoint = function (x,y) { if(!x || !y) { window.alert("GPS坐标不存在, ...
- cache和buffer区别探讨
一. 1.Buffer(缓冲区)是系统两端处理速度平衡(从长时间尺度上看)时使用的.它的引入是为了减小短期内突发I/O的影响,起到流量整形的作用.比如生产者——消费者问题,他们产生和消耗资源的速度大体 ...
- 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 ...
- Linux - vim安装 配置与使用
一 Vim 简单介绍 曾经一直用vi,近期開始使用 vim,以下将两者做一下比較. vi和vim都是word=%E5%A4%9A%E6%A8%A1&fr=qb_search_exp&i ...
- C# 为枚举创建新方法
可以使用扩展方法添加特定于某个特定枚举类型的功能. 示例在下面的示例中,Grades 枚举表示学生可能在班里收到的字母等级分.该示例将一个名为 Passing 的扩展方法添加到 Grades 类型中, ...
- RealProxy AOP过滤方法的参数
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- 【数据挖掘】分类之Naïve Bayes(转载)
[数据挖掘]分类之Naïve Bayes 1.算法简介 朴素贝叶斯(Naive Bayes)是监督学习的一种常用算法,易于实现,没有迭代,并有坚实的数学理论(即贝叶斯定理)作为支撑. 本文以拼写检查作 ...
- Android Studio 使用笔记:文件查询方法总结
搜索单词 Windows: Ctrl + F Mac : Cmd + F 会在当前激活的文件上查询输入的关键字,以高亮显示 跳转行 Windows: Ctrl + L Mac : Cmd + ...