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

简介: <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());
	}
}


 
相关文章
|
3月前
使用 for 循环输出数组
【10月更文挑战第29天】使用 for 循环输出数组。
35 3
|
5月前
|
C语言 索引 Python
利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。
利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。
116 4
|
7月前
|
语音技术
语音识别-----列表的常用操作课后练习讲解,用变量追加,取出第一个,取出最后一个,下标位置,列表的循环遍历,下标+1的写法,len下标可以小于这个值,while循环对index循环的遍历
语音识别-----列表的常用操作课后练习讲解,用变量追加,取出第一个,取出最后一个,下标位置,列表的循环遍历,下标+1的写法,len下标可以小于这个值,while循环对index循环的遍历
|
9月前
|
C#
C# 循环遍历使用
C# 循环遍历使用
172 0
|
9月前
两个整数做除法,有时会产生循环小数,其循环部分称为:循环节。 比如,11/13=6=>0.846153846153… 其循环节为[846153] 共有6位。
两个整数做除法,有时会产生循环小数,其循环部分称为:循环节。 比如,11/13=6=>0.846153846153… 其循环节为[846153] 共有6位。
114 0
|
9月前
while循环和do while循环有什么区别
while循环和do while循环有什么区别
110 0
while循环和do while循环有什么区别?
while循环和do while循环有什么区别?
161 0
三个线程循环顺序打印
三个线程循环顺序打印
90 0
|
存储 Java 测试技术
打印不重复的字符串全排列(递归)
本文将详细解析在生成不重复的字符串全排列时使用的Java代码。首先,我们将展示一个常规的全排列生成方法,然后介绍如何通过使用HashSet来跳过已经尝试过的字符,从而避免生成重复的全排列。最后,我们提供了一道相关的编程题目以供练习。
137 0
打印不重复的字符串全排列(递归)
|
安全
运算符:指数-链判断-Null判断-逻辑赋值
运算符:指数-链判断-Null判断-逻辑赋值
93 0