Determine whether a single link is cyclic and find the first cyclic Node

Source: Internet
Author: User

Introduction

Determine whether a single link is cyclic and find the first cyclic node.

Ideas

[Determine whether a single link is cyclic]: If a single link is cyclic, the cyclic part is closed. This is like a track and field. When two people start to run, although there is a certain gap between them, they will meet sooner or later.

Let's just take a mathematical model from it, one is step steps (corresponding to the distance between the two people when they started running ); one is to judge the single-chain loop condition nodex = nodey (the two "met ").

[Find the first cyclic node]: I 've thought about many other methods and it's hard to implement them. I went out for two hours and came back to think of using Hash Storage to determine the hash element inclusion, the results are easy to implement and easy to understand.

For example, it is a single loop, and the first cyclic node is 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());}}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.