1,制作环型链表

2。检測链表中是否存在环(參考文章)

3。计算链表中环的长度

4, 计算链表中环起始的位置

5,推断一个链表是否存在回文,要求O(n)时间和O(1)空间的复杂度(推断链表是否是回文,要求O(n)时间。O(1)空间)

6。计算链表中间位置

7,链表原地反转(链表原地反转)

8,測试code

#include <iostream>
using namespace std;
/*@1: detect if there is a circule in a list -> detect_circule_exist@2: calculate the circule length in the list -> give_circ_len@3: locate the place where the circule starts -> locate_circ_place@4: detect if the list is palindrome -> time:o(n) and space:o(1)
*/
/*struct simple linked list
*/
struct item
{int n;struct item *next;
};
class iter {struct item *head;int circ_len;
public:void insert(int i);bool remove(int i);iter(){this->circ_len = 0;this->head = new(struct item);this->head->next = NULL;}bool create_circule(int i);void remove_circule();bool detect_circule_exist();int give_circ_len();int locate_circ_place();bool detect_palindrome();void display();~iter();
};
void iter::insert(int i) {struct item *cur;struct item *add_item = new(struct item);add_item->n = i;add_item->next = NULL;/* insert at tail */cur = this->head;while(cur->next) {cur = cur->next;}cur->next = add_item;cout << add_item <<endl;
}
bool iter::remove(int i)
{struct item *cur = this->head->next, *prev = this->head;while (cur) {if (cur->n == i) {prev->next = cur->next;return true;} else {cur = cur->next;prev = prev->next;}}return false;
}
void iter::display()
{struct item *cur = this->head->next;while (cur){cout << cur->n << " -> ";cur = cur->next;}cout <<"end" << endl;
}/* create a circult at value: i, if not i exist return false*/
bool iter::create_circule(int i)
{struct item *cur = this->head;struct item *find = NULL;while (cur->next){cur = cur->next;if (cur->n == i) {find = cur;}}if (find) {cur->next = find;return true;} elsereturn false;
}
/* detect if exist a circule in the linked list */
bool iter:: detect_circule_exist()
{/*quick and slow point to the first element of the linked list*/struct item *quick, *slow, *mark;bool find = false;slow = this->head->next;quick = this->head->next->next;while (quick && slow) {if (!find) {if (quick == slow) {find = true;mark = slow;slow = slow->next;this->circ_len++;} else {if (!quick->next || !quick->next->next)break;if (!slow->next)break;quick = quick->next->next;slow = slow->next;}} else {if (mark == slow) {return true;} else {slow = slow->next;this->circ_len++;}}}find = false;return find;}
int iter:: give_circ_len()
{this->detect_circule_exist();return this->circ_len;
}
/* cal the len of non circule at the linked list */
int iter:: locate_circ_place()
{if (this->detect_circule_exist()) {struct item *quick, *slow;int i = this->circ_len;slow = this->head->next;quick = this->head->next;while (quick && i--) {quick = quick->next;}i = 0;while (quick && slow) {if (quick == slow) {return i;} else {i++;quick = quick->next;slow = slow->next;}}} else {return 0;}
}
void iter:: remove_circule()
{struct item *tail = this->head;struct item *mark;int i = this->locate_circ_place() + 1;while (tail->next && i--){tail = tail->next;}mark = tail;while (tail) {if (tail->next == mark) {tail->next = NULL;return;} else {tail = tail->next;}}
}
bool iter:: detect_palindrome()
{struct item *quick, *slow;/*first find the middle place of the linked list by quick and slow pointer*/slow = this->head->next;quick = this->head->next->next;while (slow) {struct item *mark;slow = slow->next;quick = quick->next->next;if (!quick) {/*reverse the linked list at the place*/mark = slow;quick = slow->next->next; /*q*/slow = slow->next; /*p*/struct item *temp;while (quick) {temp = quick->next;quick->next = slow;slow = quick;quick = temp;}mark->next->next = NULL;mark->next = slow;slow = this->head->next;mark = mark->next;while (slow && mark) {//cout << "odd first part: "<< slow->n << "addr:"  << slow << "second part: " << mark->n << "addr:" << mark <<endl;if (slow->n != mark->n)return false;else {slow = slow->next;mark = mark->next;}}return true;}if (!quick->next) {/*reverse the linked list at the place*/mark = slow;quick = slow->next->next; /*q*/slow = slow->next; /*p*/struct item *temp;while (quick) {temp = quick->next;quick->next = slow;slow = quick;quick = temp;}mark->next->next = NULL;mark->next = slow;slow = this->head->next;mark = mark->next;while (slow && mark) {//cout << "even first part: "<< slow->n << "addr:"  << slow << "second part: " << mark->n << "addr:" << mark <<endl;if (slow->n != mark->n)return false;else {slow = slow->next;mark = mark->next;}}return true;}}
}
iter::~iter()
{struct item *cur = this->head->next;if (this->detect_circule_exist())this->remove_circule();while (cur){struct item *temp = cur;cur = cur->next;delete temp;//cout <<"delete" << temp<< endl;}delete this->head;}
int main()
{class iter myiter;/*for (int i = 0; i < 10; i++) {myiter.insert(i);}myiter.create_circule(2);if (myiter.detect_circule_exist())cout << "detect circule exist and circ len" << myiter.give_circ_len()<< endl;elsecout << "no detect circule exist" <<endl;cout << "circ point index" << myiter.locate_circ_place() <<endl;myiter.remove_circule();myiter.display();*//*myiter.insert(1);myiter.insert(2);myiter.insert(3);myiter.insert(3);myiter.insert(2);myiter.insert(1);if (myiter.detect_palindrome())cout << "even detect palindrome" <<endl;elsecout << "even no detect palindrome" <<endl;*/myiter.insert(1);myiter.insert(2);myiter.insert(3);myiter.insert(2);myiter.insert(1);if (myiter.detect_palindrome())cout << "odd detect palindrome" <<endl;elsecout << "odd no detect palindrome" <<endl;return 1;
}

转载于:https://www.cnblogs.com/liguangsunls/p/6915506.html

快慢指针和链表原地反转相关推荐

  1. 【链表习题集1】整体和局部反转链表同频和快慢指针合并链表

    前言: 刷题和面试兼顾还得看你啊-牛客网 近几年互联网受疫情影响,许多互联网都使用牛客网在线笔试招人 很多同学因为不熟悉牛客网的环境和使用,最后在线笔试面试中屡屡受挫 牛客网提供了语言巩固,算法提高等 ...

  2. 【LeetCode笔记】234. 回文链表(Java、快慢指针、链表)

    文章目录 题目描述 思路 & 算法 题目描述 写这道题前最好把206.翻转链表 写了 有空间复杂度的话都好说,不管是新建链表.还是用字符串equals都好做. 思路 & 算法 快慢指针 ...

  3. 如何用快慢指针在链表找到中间点

    快慢指针是一种用于在链表中查找中间结点的技巧.具体来说,您可以定义两个指针,一个指针每次移动一个结点,另一个指针每次移动两个结点.因为快指针每次移动的结点数量是慢指针移动结点数量的两倍,所以当快指针到 ...

  4. 快慢指针判断链表中是否存在环以及查找环的起始位置

    判断链表中是否有环?    使用快慢指针, 慢指针一次走一步, 快指针一次走两步, 当快慢指针相遇时,说明链表存在环 为什么快指针每次走两步而慢指针每次走一步呢?    因为slow指针和fast指针 ...

  5. 快慢指针判断链表是否有环

    链表中很经常会出现的一个问题,判断链表是否有环,标准答案也已经烂熟于心,设置快慢指针,快指针每次走2步,慢指针每次走1步,如果两个指针可以相遇的话,证明链表有环,反之无环. 我有时候就犯嘀咕,它俩一定 ...

  6. 证明-快慢指针找链表环

    快慢指针证明 (1)先证明两个指针可以相遇 1.如果链表没有环,那么快指针比慢指针先到达尾部(指向null). 2.如果链表有环的话,因为快指针走的比慢指针快,所以在环中相遇的过程可以看作是快指针从环 ...

  7. 使用快慢指针判断链表是否有环

    有时候在面试中可能会遇到叫我们判断链表中是否有环的问题,这个问题的解决方法也很多,这里我记录一下比较简单的使用快慢指针的方法: 使用快慢指针是指: 设置两个指针,一快一慢,快指的是每次移动两步,慢指针 ...

  8. 快慢指针判断链表中是否有环

    基本思想 快指针:从头开始移动 每次移动两个距离 慢指针:从头开始移动 每次移动一个距离 如果单链表中存在有环的话,那么快慢指针一定是会相遇的. 为什么?首先快指针在环内肯定是可以追上慢指针的对吧,那 ...

  9. 【LeetCode笔记】141. 环形链表(Java、快慢指针、链表)

    文章目录 题目描述 思路 & 代码 题目描述 常见题,用上了久违的快慢指针 思路 & 代码 举个例子就能明白了: 我和汽车,进行一场比赛,跑道可能是环形跑道,也可能是直道. 直道的话, ...

最新文章

  1. 代码版本控制工具Concurrent Versions System(CVS)的三种用配置库更新本地工作目录文件的方法
  2. SQL Server 2008中原生的分层数据类型:hierarchyid
  3. VS2010打不开创建的项目的解决方法
  4. 计算机16进制应用,16进制计算器安装方法 16进制计算器使用技巧
  5. C++ Opengl 绘制图像字符源码
  6. Java读取文件时第一行出现乱码“?”问号
  7. [国嵌攻略][125][总线设备驱动模型]
  8. 大数据之_Hadoop工作笔记001---Hadoop3.1.2在Centos7上安装_单机版
  9. Linux基础-5进程管理工具
  10. [20171106]配置客户端连接注意.txt
  11. HDU1131 Java大数
  12. C++习题之变量和基本类型
  13. 基于 Flink 打造的伴鱼实时计算平台 Palink 的设计与实现
  14. 2018年泰迪杯数据挖掘比赛c题
  15. 启动程序时提示缺少mfc.dll
  16. android SDK安装以及环境变量配置(windows)
  17. ECharts为X轴Y轴添加单位
  18. matlab差值后求斜率,用三次样条插值求离散点斜率 matlab程序
  19. eclipse安装angularjs
  20. 扶贫?教育?地铁?这里有跨界也能实现的企业数字化新操作!

热门文章

  1. 理解生成模型与判别模型
  2. 高级前端常见手写面试题指南
  3. Mac 鼠标和触摸板左键突然失效
  4. 使用Python求根据提成计算的奖金
  5. 创龙基于TI AM335x ARM Cortex-A8 CPU,主频高达1GHz开发板FRAM、电源接口和拔码开关
  6. Ubuntu下载及使用opencv对图像及视频的处理方法
  7. 看好老东家发布的鸿蒙操作系统(HarmonyOS)
  8. php数组循环添加键值对
  9. 解决Android NDK 报jxxx编译找不到
  10. 通过Request获取客户端的真实IP