Data Structure Linked List: Detect and Remove Loop in a Linked List
http://www.geeksforgeeks.org/detect-and-remove-loop-in-a-linked-list/
#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 detectandremove(node *&head) {
node *p, *q;
p = q = head;
while (q && q->next) {
q = q->next->next;
p = p->next;
if (p == q) break;
}
if (!q || !q->next) return; //no loop
q = head;
while (q != p) {
q = q->next;
p = p->next;
} //get the starting point of the ring
while (q->next != p) q = q->next; //get the point before the starting point of the ring
q->next = NULL; //remove the ring
} void print(node *head) {
while (head) {
cout << head->data << " ";
head = head->next;
}
} int main() {
node *head = NULL;
push(head, );
push(head, );
push(head, );
push(head, );
push(head, );
push(head, );
head->next->next->next->next->next->next = head->next->next->next;
detectandremove(head);
print(head);
return ;
}
Data Structure Linked List: Detect and Remove Loop in a Linked List的更多相关文章
- [转]Data Structure Recovery using PIN and PyGraphviz
Source:http://v0ids3curity.blogspot.com/2015/04/data-structure-recovery-using-pin-and.html --------- ...
- [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 ...
- 211. Add and Search Word - Data structure design
题目: Design a data structure that supports the following two operations: void addWord(word) bool sear ...
- [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 ...
- Python: tree data structure
# 树结构 from pythonds.basic.stack import Stack #pip install pythonds from pythonds.trees.binaryTree im ...
- 面试总结之数据结构(Data Structure)
常用数据结构及复杂度 http://www.cnblogs.com/gaochundong/p/3813252.html 常用数据结构的时间复杂度 Data Structure Add Find De ...
- 笔记-python-tutorial-5.data structure
笔记-python-tutorial-5.data structure 1. data structure 1.1. list operation list.append(x) #尾部 ...
随机推荐
- js中的四舍五入函数
刚学到这部分时,感觉特别简单.可最近写个ajax分页时,忽然忘记应该怎么使用哪种函数来计算总的页数...哎,好记星不如烂笔头啊,还是老老实实的写下来吧.随时查看. 1.Math.ceil(x):对x向 ...
- win 下g++ 安装、环境配置等
工具:eclipse for c++: 由于eclipse没有集成c++的编译器及执行环境,所以须要自己额外安装g++等. 方法: 使用MinGW来下载和安装须要的工具: 下载地址:http://ww ...
- opencv--python--anaconda----contrib 安装
https://pypi.python.org/pypi/opencv-python/3.4.0.12 https://pypi.python.org/pypi?%3Aaction=search&am ...
- leetCode 50.Pow(x, n) (x的n次方) 解题思路和方法
Pow(x, n) Implement pow(x, n). 思路:题目不算难.可是须要考虑的情况比較多. 详细代码例如以下: public class Solution { public doubl ...
- Spring Boot内嵌Tomcat session超时问题
最近让Spring Boot内嵌Tomcat的session超时问题给坑了一把. 在应用中需要设置session超时时间,然后就习惯的在application.properties配置文件中设置如下, ...
- kafka官方Quick Start
1.下载kafka,并上传到服务器 2.如果之前没安装zookeeper,这里可以启动一个简单的zookeeper bin/zookeeper-server-start.sh config/zooke ...
- 蒙特卡洛方法计算圆周率的三种实现-MPI openmp pthread
蒙特卡洛方法实现计算圆周率的方法比较简单,其思想是假设我们向一个正方形的标靶上随机投掷飞镖,靶心在正中央,标靶的长和宽都是2 英尺.同时假设有一个圆与标靶内切.圆的半径是1英尺,面积是π平方英尺.如果 ...
- SecureCRT 设置字体跟颜色
SecureCRT 绝佳配色方案, 保护你的眼睛 分类: Linux 软件使用2013-05-17 08:45 24038人阅读 评论(11) 收藏 举报 SecureCRT 绝佳配色方案, 保护你的 ...
- @media screen 针对不同移动设备——响应式设计
概念: device-pixel-ratio:定义输入设备屏幕的可视宽度与可见高度比率. device-width:输入设备屏幕的可视宽度. orientation :屏幕横竖屏定向.landscap ...
- Django安装和启动
1.django安装 在http://www.djangoproject.com/download/这个网站上可以下载django的最新版本.在下载时,要注意django版本和本机安装的Python版 ...