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) #尾部 ...
随机推荐
- css3中的新特性经典应用
这篇文章主要分析css3新特性的典型应用,都是干活,没得水分. 1.动画属性:animation. 利用animation可以实现元素的动画效果,他是一个简写属性,用于设置6个动画属性:aminati ...
- linux程序设计——取消一个线程(第十二章)
12.7 取消一个线程 有时,想让一个线程能够要求还有一个线程终止,就像给它发送一个信号一样. 线程有方法能够做到这一点,与与信号处理一样.线程能够被要求终止时改变其行为. pthread_ca ...
- 了解.net mvc实现原理ActionResult/View
了解.net mvc实现原理ActionResult/View 上一篇了解了请求至Controller的Action过程,这篇继续看源码处理Action收到请求数据再返回ActionResult到Vi ...
- Android开发之Conversion to Dalvik format failed问题解决
[2014-4-21 21:28:06 - Dex Loader] Unable to execute dex: java.nio.BufferOverflowException. Check the ...
- Eclipse个最实用的快捷键
一个Eclipse骨灰级开发人员总结了他觉得最实用但又不太为人所知的快捷键组合.通过这些组合能够更加easy的浏览源码,使得总体的开发效率和质量得到提升. 1. ctrl+shift+r:打开 ...
- CentOS yum 命令出现 [Errno 14] curl#6 - "Couldn't resolve host ..." 的解决方法
安装svn的时候,发现报错说一个地址无法訪问. # yum list | grep subversion http://opensource.wandisco.com/centos/7/svn-1.8 ...
- java.lang.NoClassDefFoundError: ch/qos/logback/core/joran/spi/JoranException
问题描述:启动tomcat服务器的时候,报找不到JoranException类的异常 原因:tomcat中没有logback-core-1.1.2.jar包 解决方法:在tomcat中的lib目录添加 ...
- 搭建redis集群遇到的坑
搭建redis集群遇到的坑 #!/bin/bash # 作者: tuhooo # 日期: 2017.4.23 20.15 # 用途: 通过ruby脚本启动redis伪集群 if [ $2 == &qu ...
- Spring读书笔记-----Spring核心机制:依赖注入
spring框架为我们提供了三种注入方式,分别是set注入,构造方法注入,接口注入.今天就和大家一起来学习一下 依赖注入的基本概念 依赖注入(Dependecy Injection),也称为IoC(I ...
- Idea中优化Markdown Support显示效果
转自:https://www.jianshu.com/p/d093c42a8c29 因为工作中为提高工作效率,我一般习惯于直接在`idea`中使用`markdow support`插件来进行相关文档的 ...