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. 合并apk和odex

    Android的ROM中有很多odex文件,相对于APK中的dex文件而言这个odex有什么作用呢? 如果你仔细观察会发现文件名时一一对应的,同时那些对应的apk文件中没有dex文件.这样做可以使其厂 ...

  2. 企业级分布式存储应用与实战MogileFS、FastDFS

    项目实战9—企业级分布式存储应用与实战MogileFS.FastDFS   目录 实战一:企业级分布式存储应用与实战 mogilefs 实现 原理 1.环境准备 2.下载安装,每个机器都一样 3.数据 ...

  3. windows快捷启动命令

    gpedit.msc-----组策略 sndrec32-----录音机 nslookup----- ip地址侦测器 explorer------ 打开资源管理器 logoff-------注销命令 t ...

  4. sql自定义函数及C#中调用

    1.在C#中调用sql自定义函数 1.1 标量值函数 sql语句调用 select  dbo.GetClassIDWithName(1) string strSql = string.Format(& ...

  5. 安装Reshaper后Intellisense失效

    安装Reshaper后Intellisense失效或希望用vs2017的Intellisense功能 安装完毕后,IDE 的智能提示(Intellisense)便会默认使用 Resharper 的提示 ...

  6. SpringCloud系列四:实现Eureka Server的高可用并将应用注册到Eureka Sever集群上

    1. 回顾 上一篇博客中,实现了单节点的Eureka Server.Eureka Client会定时连接Eureka Server,获取注册表中的信息并缓存到本地.微服务在消费远程API时总是使用本地 ...

  7. struts2在项目中的应用之下载

    文件下载是一个非经常见的功能,用struts2实现文件下载的步骤: 一)定义一个Action类.FileDownload.java package com.struts2.filedownload; ...

  8. spring boot 集成mybatis报错

    Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of ...

  9. 快速搭建一个成熟,强壮的App框架【转载】

    App框架搭建 招聘信息: iOS 研发工程师 iOS开发工程师 iOS开发实习工程师 新浪微博-Android开发工程师&iOS开发工程师 美术设计师(2D) UI设计师 cocos2dx手 ...

  10. C++11 并发指南三(Lock 详解)(转载)

    multithreading 多线程 C++11 C++11多线程基本使用 C++11 并发指南三(Lock 详解) 在 <C++11 并发指南三(std::mutex 详解)>一文中我们 ...