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 ...
随机推荐
- ”ftp使用dos命令“
ftp不能使用dos命令,ftp有专用的命令. 在批处理文件中,如果用到dos命令获取信息(比如:系统日期),将用获取的信息,输出到ftp脚本文件中,然后执行ftp脚本文件. set yyyy=%DA ...
- 通用对话弹窗CommonDialog
代码地址如下:http://www.demodashi.com/demo/12592.html 通用对话弹窗CommonDialog Version 1.0 Created by chenchangj ...
- Jmeter返回org.apache.http.NoHttpResponseException: The target server failed to respond解决办法
1.问题:这段时间使用jmeter压测时(300个请求,持续压300s,加速期10s),压力平稳时偶然错误,部分请求失败事务报错信息如下 org.apache.http.NoHttpResponseE ...
- css 用 display: inline-block; 代替 float
浮动可以将两个块级元素浮动在同一水平上.但float的缺点也有很多,还需要其他样式弥补. 早年我使用过 display: inline-block; 但苦于兼容性问题一直没敢全面使用. 近几年主要玩移 ...
- eclipse JVM Tomcat 内存堆栈大小设置
1, 设置Eclipse内存使用情况 修改eclipse根目录下的eclipse.ini文件 -vmargs //虚拟机设置 -Xms40m //初始内存 -Xmx256m //最大内存 -Xmn ...
- 09 nginx Rewrite(重写)详细解析
一:Rewrite(重写)详细解析 rewrite 重写 重写中用到的指令 if (条件) {} 设定条件,再进行重写 set #设置变量 return #返回状态码 break #跳出rewri ...
- postman 测试API - 获取/调用 token
1.使用全局变量保存token 2.再调用 参考文章 http://www.jianshu.com/p/13c8017bb5c8 https://testerhome.com/topics/6555
- 开源项目之easyrtmp
https://github.com/bigbluebutton86/EasyRTMP/tree/master/src http://dl.linux-sunxi.org/SDK/A20/A20_SD ...
- Intellij idea 切换SVN路径
一直不懂如何切换路径,每次都是删除---->检出:本地源码都不能保存下来,非常麻烦 //在idea中svn切换到新分支:[vcs] -> [subversion] -> [updat ...
- java 十进制转十六进制、十进制转二进制、二进制转十进制、二进制转十六进制
//10进制转16进制 Integer.toHexString(20); //10进制转2进制 Integer.toBinaryString(10); //16进制转10进制 Integer.pars ...