判断单链是否循环,并且找出第一个循环节点

简介: <p>介绍</p> <p>    判断单链是否循环,并且找出第一个循环节点。</p> <p>思路</p> <p>    【判断单链是否循环】:如果单链是循环的,那么循环部分就是封闭的。这好比一个田径运动场,当两个人跑步时,开始虽然有一定的间距,但他们迟早会相遇的。</p> <p>顺其自然的我们从中抽取一个数学模型,一个是步长Steps(对应两人刚开始跑步时的间距);一个是判断单链循

介绍

    判断单链是否循环,并且找出第一个循环节点。

思路

    【判断单链是否循环】:如果单链是循环的,那么循环部分就是封闭的。这好比一个田径运动场,当两个人跑步时,开始虽然有一定的间距,但他们迟早会相遇的。

顺其自然的我们从中抽取一个数学模型,一个是步长Steps(对应两人刚开始跑步时的间距);一个是判断单链循环的条件nodeX==nodeY(两人“相遇”)。

    【找出第一个循环节点】:我想过好多其它方法,实现起来都比较难,后来出去骑行了两个小时,回来后就想到借助Hash存储,Hash元素包含判断,结果实现起来既容易,又好懂。

    比如:下图就是一个循环单链,第一个循环节点为3。


package shuai.study.link.circle;

import java.util.HashSet;
import java.util.Set;

class Node {
	String data = null;
	Node nextNode = null;

	public Node(String data, Node nextNode) {
		this.data = data;
		this.nextNode = nextNode;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

class SingleCircleLink {
	Node firstNode = null;

	Node nodeX = null;
	Node nodeY = null;

	public SingleCircleLink(Node firstNode) {
		this.firstNode = firstNode;

		this.nodeX = firstNode;
		this.nodeY = firstNode;
	}

	public boolean isSingleCircleLink() {
		/*
		 * This while block judge whether this link is circle or not.
		 */
		while (nodeY != null && (nodeY = nodeY.nextNode) != null && nodeX != nodeY) {
			nodeX = nodeX.nextNode;
			nodeY = nodeY.nextNode;
		}

		/*
		 * if Node.next is null, then it illustrates this link isn't circle link.
		 */
		return nodeY != null;
	}

	public void printFirstCircleNode(boolean isSingleCircleLinkFlag) {
		if (isSingleCircleLinkFlag) {
			System.out.println("This is single circle link");

			Set<Node> hashSet = new HashSet<Node>();
			Node nodeZ = firstNode;

			/*
			 * This while block will find the first circle node.
			 */
			while (true) {
				hashSet.add(nodeZ);
				nodeZ = nodeZ.nextNode;

				if (hashSet.contains(nodeZ)) {
					break;
				}
			}

			System.out.println("The first circle node is " + nodeZ.data);
		} else {
			System.out.println("This is not single circle link");
		}
	}
}

/////////////////////////////////////////////////////////////////////////////////////////

/**
 * @author shengshu
 * 
 */
public class SingleCircleLinkApp {
	public static void main(String[] args) {
		Node node6 = new Node("6", null);
		Node node5 = new Node("5", node6);
		Node node4 = new Node("4", node5);
		Node node3 = new Node("3", node4);
		Node node2 = new Node("2", node3);
		Node node1 = new Node("1", node2);

		node6.nextNode = node3;

		SingleCircleLink singleCircleLink = new SingleCircleLink(node1);
		singleCircleLink.printFirstCircleNode(singleCircleLink.isSingleCircleLink());
	}
}


 
相关文章
|
4月前
使用 for 循环输出数组
【10月更文挑战第29天】使用 for 循环输出数组。
46 3
|
8月前
|
语音技术
语音识别-----列表的常用操作课后练习讲解,用变量追加,取出第一个,取出最后一个,下标位置,列表的循环遍历,下标+1的写法,len下标可以小于这个值,while循环对index循环的遍历
语音识别-----列表的常用操作课后练习讲解,用变量追加,取出第一个,取出最后一个,下标位置,列表的循环遍历,下标+1的写法,len下标可以小于这个值,while循环对index循环的遍历
|
10月前
|
C#
C# 循环遍历使用
C# 循环遍历使用
176 0
|
10月前
两个整数做除法,有时会产生循环小数,其循环部分称为:循环节。 比如,11/13=6=>0.846153846153… 其循环节为[846153] 共有6位。
两个整数做除法,有时会产生循环小数,其循环部分称为:循环节。 比如,11/13=6=>0.846153846153… 其循环节为[846153] 共有6位。
122 0
|
10月前
while循环和do while循环有什么区别
while循环和do while循环有什么区别
135 0
while循环和do while循环有什么区别?
while循环和do while循环有什么区别?
172 0
三个循环(C)
while语句:while(表达式){},先判断表达式,若符合,则执行循环内容,一个循环后再次判断表达式。
89 0
|
JavaScript 前端开发
不同类型的循环
不同类型的循环
90 0
|
存储 算法
打印N个数的循环算法和递归算法比较
打印N个数的循环算法和递归算法比较
|
Java Shell Linux
Shell流程控制(重点)、if 判断、case 语句、let用法、for 循环中有for (( 初始值;循环控制条件;变量变化 ))和for 变量 in 值 1 值 2 值 3… 、while 循环
Shell流程控制(重点)、if 判断、case 语句、let用法、for 循环中有for (( 初始值;循环控制条件;变量变化 ))和for 变量 in 值 1 值 2 值 3… 、while 循环
Shell流程控制(重点)、if 判断、case 语句、let用法、for 循环中有for (( 初始值;循环控制条件;变量变化 ))和for 变量 in 值 1 值 2 值 3… 、while 循环