Data Structure Linked List: Function to check if a singly linked list is palindrome
2024-08-25 12:48:38
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 ;
}
最新文章
- 算是休息了这么长时间吧!准备学习下python文本处理了,哪位大大有好书推荐的说下!
- Redis设置认证密码 Redis使用认证密码登录 在Redis集群中使用认证密码
- PHP小总结
- yum常用命令
- 如何让Notepad++添加Python运行方式.精讲
- 进程控制块(Process Control Block, PCB)
- UML用例图中泛化、扩展、包括
- 点餐APP 冲刺三总结
- POJ 2065 SETI (高斯消元 取模)
- C# Json数据反序列化为Dictionary并根据关键字获取指定值1
- APP的测试过程和重点
- STM8S学习笔记-时钟控制2
- ActionBar +Tab+ViewPager +Fragment 支持侧滑动完成办税工具的页面展示
- mysql 简单游标
- php反射类 ReflectionClass
- javascript之事件处理
- (NO.00005)iOS实现炸弹人游戏(八):游戏主角(一)
- 13.Git分支-变基(rebase)、rebase VS merge
- Masonry 与 frame 混用导致的问题
- Web概述