数据结构之链表面试题汇总(三)判断单链表是否有环、取出环的起始点、得到有环链表中环的长度
2017-11-02 21:17
603 查看
title: 数据结构之链表面试题汇总(三)判断单链表是否有环、取出环的起始点、得到有环链表中环的长度
date: 2016-08-18 9:14:00
categories: 数据结构
版权声明:本站采用开放的[知识共享署名-非商业性使用-相同方式共享 许可协议]进行许可
所有文章出现的代码,将会出现在我的github中,名字可以根据类全名来找,我在github中的文件夹也会加目录备注。
首先判断链表是否为空,为空抛异常
利用”双指针“方法:
一个指针每次移动一个节点,另外一个指针每次移动两个节点,如果第一个指针所对应的节点等于第二个指针所对应的节点,那么这个链表有环
在这里需要注意传入来链表的节点数
图解:
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%88%A4%E6%96%AD%E9%93%BE%E8%A1%A8%E6%98%AF%E5%90%A6%E6%9C%89%E7%8E%AF.png)
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%88%A4%E6%96%AD%E9%93%BE%E8%A1%A8%E6%98%AF%E5%90%A6%E6%9C%89%E7%8E%AF2.png)
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%88%A4%E6%96%AD%E9%93%BE%E8%A1%A8%E6%98%AF%E5%90%A6%E6%9C%89%E7%8E%AF3.png)
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%88%A4%E6%96%AD%E9%93%BE%E8%A1%A8%E6%98%AF%E5%90%A6%E6%9C%89%E7%8E%AF4.png)
代码实现:
测试代码:有环
运行结果:
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%88%A4%E6%96%AD%E9%93%BE%E8%A1%A8%E6%98%AF%E5%90%A6%E6%9C%89%E7%8E%AF%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C.png)
测试代码:无环:即把第五个节点的设置下一个节点的代码注释掉
运行结果:
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%88%A4%E6%96%AD%E9%93%BE%E8%A1%A8%E6%98%AF%E5%90%A6%E6%9C%89%E7%8E%AF%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C2.png)
可以借助上一题中判断是否有环的方法,在循环中判断first=second的时候,如果相等,直接返回first或者second即可。
代码实现:
测试代码:有环
运行结果:
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%BE%97%E5%88%B0%E6%9C%89%E7%8E%AF%E9%93%BE%E8%A1%A8%E7%8E%AF%E5%BC%80%E5%A7%8B%E8%8A%82%E7%82%B9%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C.png)
无环测试代码:
运行结果:
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%BE%97%E5%88%B0%E6%9C%89%E7%8E%AF%E9%93%BE%E8%A1%A8%E7%8E%AF%E5%BC%80%E5%A7%8B%E8%8A%82%E7%82%B9%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C2.png)
如果没有环,返回的是null,如果要读取里面的数据,就会抛空指针异常
继续继承判断链表是否有环的方法,只不过在判断的时候,把一个链表的头节点传到函数中
首先获取该链表的环开始的节点
再定义两个变量来记录返回节点信息,即环开始的节点
一个留作备用,另外一个用来比较
再定义一个变量来记录环的长度
循环是死循环,因为有环链表永远到不了头
通过判断现在的节点是否等于环开始节点来跳出循环,并且返回环长度
代码实现:
测试代码:有环
运行结果:
![](http://7xritp.com1.z0.glb.clouddn.com/%E8%8E%B7%E5%8F%96%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%8E%AF%E7%9A%84%E9%95%BF%E5%BA%A6.png)
测试代码:无环:
运行结果:
date: 2016-08-18 9:14:00
categories: 数据结构
版权声明:本站采用开放的[知识共享署名-非商业性使用-相同方式共享 许可协议]进行许可
所有文章出现的代码,将会出现在我的github中,名字可以根据类全名来找,我在github中的文件夹也会加目录备注。
判断单链表是否有环
思路:首先判断链表是否为空,为空抛异常
利用”双指针“方法:
一个指针每次移动一个节点,另外一个指针每次移动两个节点,如果第一个指针所对应的节点等于第二个指针所对应的节点,那么这个链表有环
在这里需要注意传入来链表的节点数
图解:
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%88%A4%E6%96%AD%E9%93%BE%E8%A1%A8%E6%98%AF%E5%90%A6%E6%9C%89%E7%8E%AF.png)
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%88%A4%E6%96%AD%E9%93%BE%E8%A1%A8%E6%98%AF%E5%90%A6%E6%9C%89%E7%8E%AF2.png)
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%88%A4%E6%96%AD%E9%93%BE%E8%A1%A8%E6%98%AF%E5%90%A6%E6%9C%89%E7%8E%AF3.png)
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%88%A4%E6%96%AD%E9%93%BE%E8%A1%A8%E6%98%AF%E5%90%A6%E6%9C%89%E7%8E%AF4.png)
代码实现:
public static boolean hasLoopReturnBoolean(Node head) { Node first = head; Node second = head; // if the head is null return exception if (head == null || head.getNext() == null) return false; // move first on step backward and the second move twice at once // if the condition of the judgement without the second.getNext()!=null // when it reach the tail it will raise the NPE while (second != null && second.getNext() != null) { first = first.getNext(); second = second.getNext().getNext(); // if first=second return true if (first == second) return true; } // else false return false; }
测试代码:有环
public class ReverseLinkedListTest { public static void main(String[] args) { Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); Node node4 = new Node(4); Node node5 = new Node(5); node1.setNext(node2); node2.setNext(node3); node3.setNext(node4); node4.setNext(node5); node5.setNext(node3); System.out.println(HasLoop.hasLoopReturnBoolean(node1)); } }
运行结果:
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%88%A4%E6%96%AD%E9%93%BE%E8%A1%A8%E6%98%AF%E5%90%A6%E6%9C%89%E7%8E%AF%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C.png)
测试代码:无环:即把第五个节点的设置下一个节点的代码注释掉
public class ReverseLinkedListTest { /** * @param args */ public static void main(String[] args) { Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); Node node4 = new Node(4); Node node5 = new Node(5); node1.setNext(node2); node2.setNext(node3); node3.setNext(node4); node4.setNext(node5); // node5.setNext(node3); System.out.println(HasLoop.hasLoopReturnBoolean(node1)); } }
运行结果:
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%88%A4%E6%96%AD%E9%93%BE%E8%A1%A8%E6%98%AF%E5%90%A6%E6%9C%89%E7%8E%AF%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C2.png)
有环单链表中,得到环的起始点
思路:可以借助上一题中判断是否有环的方法,在循环中判断first=second的时候,如果相等,直接返回first或者second即可。
代码实现:
public static Node hasLoopReturnNode(Node head) { Node first = head; Node second = head; // if the head is null return exception if (head == null || head.getNext() == null) return null; // move first on step backward and the second move twice at once // if the condition of the judgement without the second.getNext()!=null // when it reach the tail it will raise the NPE while (second != null && second.getNext() != null) { first = first.getNext(); second = second.getNext().getNext(); // if first=second return true if (first == second) return first; } // else without loop return null; }
测试代码:有环
public class ReverseLinkedListTest { /** * @param args */ public static void main(String[] args) { Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); Node node4 = new Node(4); Node node5 = new Node(5); node1.setNext(node2); node2.setNext(node3); node3.setNext(node4); node4.setNext(node5); node5.setNext(node3); System.out.println(HasLoop.hasLoopReturnNode(node1).getRecord()); } }
运行结果:
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%BE%97%E5%88%B0%E6%9C%89%E7%8E%AF%E9%93%BE%E8%A1%A8%E7%8E%AF%E5%BC%80%E5%A7%8B%E8%8A%82%E7%82%B9%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C.png)
无环测试代码:
public class ReverseLinkedListTest { /** * @param args */ public static void main(String[] args) { Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); Node node4 = new Node(4); Node node5 = new Node(5); node1.setNext(node2); node2.setNext(node3); node3.setNext(node4); node4.setNext(node5); // node5.setNext(node3); System.out.println(HasLoop.hasLoopReturnNode(node1).getRecord()); } }
运行结果:
![](http://7xritp.com1.z0.glb.clouddn.com/%E5%BE%97%E5%88%B0%E6%9C%89%E7%8E%AF%E9%93%BE%E8%A1%A8%E7%8E%AF%E5%BC%80%E5%A7%8B%E8%8A%82%E7%82%B9%E8%BF%90%E8%A1%8C%E7%BB%93%E6%9E%9C2.png)
如果没有环,返回的是null,如果要读取里面的数据,就会抛空指针异常
得到有环链表中环的长度
思路:继续继承判断链表是否有环的方法,只不过在判断的时候,把一个链表的头节点传到函数中
首先获取该链表的环开始的节点
再定义两个变量来记录返回节点信息,即环开始的节点
一个留作备用,另外一个用来比较
再定义一个变量来记录环的长度
循环是死循环,因为有环链表永远到不了头
通过判断现在的节点是否等于环开始节点来跳出循环,并且返回环长度
代码实现:
public static int getCycleLength(Node head) { // define the variable to record the length int length = 0; // judge the list has the loop or not Node loop = hasLoopReturnNode(head); // juege the head is null, if (loop == null) // true return 0 return length; // while the head=current the return the length Node flat = loop; Node current = loop; while (current != null) { current = current.getNext(); length++; if (current == flat) { return length; } } return length; }
测试代码:有环
public class ReverseLinkedListTest { /** * @param args */ public static void main(String[] args) { Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); Node node4 = new Node(4); Node node5 = new Node(5); node1.setNext(node2); node2.setNext(node3); node3.setNext(node4); node4.setNext(node5); node5.setNext(node3); System.out.println(HasLoop.getCycleLength(node1)); } }
运行结果:
![](http://7xritp.com1.z0.glb.clouddn.com/%E8%8E%B7%E5%8F%96%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%8E%AF%E7%9A%84%E9%95%BF%E5%BA%A6.png)
测试代码:无环:
public class ReverseLinkedListTest { /** * @param args */ public static void main(String[] args) { Node node1 = new Node(1); Node node2 = new Node(2); Node node3 = new Node(3); Node node4 = new Node(4); Node node5 = new Node(5); node1.setNext(node2); node2.setNext(node3); node3.setNext(node4); node4.setNext(node5); // node5.setNext(node3); System.out.println(HasLoop.getCycleLength(node1)); } }
运行结果:
![](http://7xritp.com1.z0.glb.clouddn.com/%E8%8E%B7%E5%8F%96%E9%93%BE%E8%A1%A8%E4%B8%AD%E7%8E%AF%E7%9A%84%E9%95%BF%E5%BA%A62.png)
相关文章推荐
- 单链表的创建和遍历、求单链表中节点的个数、查找单链表中的中间结点、判断单链表是否有环、取出有环链表中环的长度,删除有序链表中的重复结点
- 链表面试题----判断一个单链表是否带环,若带环,求入口点和环的长度
- 数据结构之链表面试题汇总(四)得到两个单链表相交的第一个交点、用O(1)的时间效率删除单向链表中的指定节点
- 判断链表中是否有环 ----- 有关单链表中环的问题
- 四道有趣的单链表面试题(单链表反序、找出链表的中间元素、链表排序、判断一个单链表是否有环)
- 判断链表中是否有环 ----- 有关单链表中环的问题
- 剑指offer面试题-判断链表是否带环并求环的入口点和环的长度
- 判断链表中是否有环 ----- 有关单链表中环的问题
- 四道有趣的单链表面试题(单链表反序、找出链表的中间元素、链表排序、判断一个单链表是否有环)
- 面试题:判断单链表是否为循环链表-快慢指针
- 判断链表中是否有环 ----- 有关单链表中环的问题
- 判断链表中是否有环 ----- 有关单链表中环的问题
- 【转载】判断链表中是否有环 ----- 有关单链表中环的问题
- 判断链表中是否有环 ----- 有关单链表中环的问题
- 判断链表中是否有环 ----- 有关单链表中环的问题
- 判断链表中是否有环 -- 有关单链表中环的问题
- 四道有趣的单链表面试题(单链表反序、找出链表的中间元素、链表排序、判断一个单链表是否有环) (转)
- 数据结构和算法设计专题之---判断单链表中是否有环,环的长度,环的入口节点
- 【数据结构】C语言判断链表是否为空,计算链表长度及链表排序算法
- 四道有趣的单链表面试题(单链表反序、找出链表的中间元素、链表排序、判断一个单链表是否有环)