Get the App
SLTechnology News&Howtos  ›  Development  › 

How does Java judge that a linked list has rings?

Shulou Source: shulou.com Published: 2022-06-01 12:53:43 09月25日 Update

This article mainly introduces Java how to judge a linked list has a ring, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Linked list is one of several commonly used data structures. The linked list structure can make full use of computer memory space and realize smart memory dynamic management. This article will show you how to determine whether a linked list is a linked list and the entry point of a linked list through Java code.

1. First define a single linked list

Var and next are attributes in a single linked list, representing the node value and the direction of the next node, respectively.

The code is as follows:

/ / define a linked list class List {public int var; public List next;// Parametric Construction public List (int var) {this.var = var;} / / No-parameter Construction public List () {} / / create a linked list public List Create () {List a = new List (1); List b = new List (2); List c = new List (3) List d = new List (4); List e = new List (5); List f = new List (6); a.next = b; b.next = c; c.next = d; d.next = e; e.next = f; f.next = d; return a;} 2. Write to determine whether there is a ring.

If it exists, the node is returned, if it does not exist, null is returned, and the speed pointer is defined. If the fast pointer catches up with the slow pointer, then there must be a ring in the linked list. If it is not caught up, or both are null, then the linked list has no ring.

The code is as follows:

/ / determine whether there is a ring, and find the node public List MeetingNode (List node) {List slow= new List (); List fast= new List (); if (node==null) return null; slow= node.next; if (slow==null) return null; fast=slow.next; while (fastening rings) {if (fast==slow) {return fast; / / fast catches up with slow and determines that it is a linked list with rings } slow = slow.next; fast = fast.next; if (fastening null) {fast = fast.next;}} return null;} 3. Find the entry node

First let the fast pointer walk the number of nodes of the ring, and then let the slow pointer begin to walk. If the two pointers meet, then the node that meets must be the entrance node of the ring.

The code is as follows:

Public List Enterdear (List node) {if (node==null) return null; if (MeetingNode (node) = = null) return null; int count = 1; List res2; List res1 = MeetingNode (node); while (res1.nextbread meeting MeetingNode (node)) {res1 = res1.next; count++;} res1 = node; for (int I = 0bot I

Tags: Node pointer code article entry memory structure chain two number value interest function dynamic at the same time attributes commonly used inevitable fast and slow point to Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Redmi Apple Shulou Information Linux