Determining whether or not a ring-way linked list linked list, if the calculated ingress node and the loop length

There is a one-way linked list, there may be a "ring," How is the program to determine whether the list loop chain?
If the ring is a list, if the list loop there, and the calculated length ingress node ring.

The ring is assumed that the following list:

5-> 3-> 7-> 2->. 6
           | |
          . 1 <-8

Ideas:

1. How to determine whether the application of the chain ring chain?

  Create two pointers p1, p2 (target node) node points to the list header, and then moves backward a p1 each node, each mobile two nodes p2 and p1 and p2 are the same comparison. If the same, then the linked list is a list loop.

 

2. How to calculate the length and the ring node ring?

  1. Create a map object, the first head node (data value) is added to the map;
  2. Then each node of the next node (data value) is added to a map object;
  3. Whether each node is added to the map, the first map to determine the key already exists next node of this node:
    1. Does not exist, then added: map the key = Data node, a map of the increment value is int value (index value may be considered a position of the node in the map);
    2. Exists, the location in ring found: = next ingress node of the current node, (the index value of the index value +1 -next node of the current node) = loop length.


Code is implemented as follows:

. 1  public  class IsCycleList {
 2      / ** 
. 3       * building node
 . 4       * / 
. 5      Private  static  class the Node {
 . 6          int Data;
 . 7          the Node Next;
 . 8          the Node ( int Data) {
 . 9              the this .data = Data;
 10          }
 . 11      }
 12 is  
13 is      / ** 
14       * P1 each time a node is moved backward, p2 move backward two nodes
 15       * @param head
 16       * @return 
. 17       * /
18     public static boolean isCycle(Node head){
19         Node p1 = head;
20         Node p2 = head;
21         while (p2 != null && p2.next != null){
22             p1 = p1.next;
23             System.out.print("p1: " +  p1.data +", ");
24             p2 = p2.next.next;
25             System.out.println("p2: " +  p2.data);
26             if (p1 == p2){
27                 return true;
28             }
 29          }
 30          return  to false ;
 31 is      }
 32  
33 is      / ** 
34 is       * if there chain ring, and calculates the length of ingress node rings
 35       * @param head
 36       * @return 
37 [       * / 
38 is      public  static the Map <String, Object> cycleLength (the Node head) {
 39          the Map <String, Object> The resultMap = new new the HashMap <> ();
 40          the Node P = head;
 41 is          the Map <Integer, Integer> = indexMap new new the HashMap ();
42         int i = 0;
43         indexMap.put(p.data,i++);
44         while(p.next != null){
45             if (indexMap.get(p.data) != null && indexMap.get(p.next.data) != null){
46                 int end = indexMap.get(p.data);
47                 int begin = indexMap.get(p.next.data);
48                 resultMap.put("length",end-begin + 1);
49                 resultMap.put("enterNode",p.next.data);
50                 return resultMap;
51             }
52             p = p.next;
53             indexMap.put(p.data,i++);
54         }
55         return resultMap;
56     }
57 
58     public static void main (String[] args){
59         Node node1 = new Node(5);
60         Node node2 = new Node(3);
61         Node node3 = new Node(7);
62         Node node4 = new Node(2);
63         Node5 = Node new new Node (6 );
 64          Node node6 = new new Node (8 );
 65          Node Node7 = new new Node (1 );
 66          Node1.Next = node2;
 67          Node2.Next = node3;
 68          Node3.Next = node4;
 69          Node4.Next = node5;
 70          Node5.Next = node6;
 71          Node6.Next = Node7;
 72          Node7.Next = node4;   // 
73 boolean result = isCycle(node1);
74 System.out.println("========"); 75 System.out.println("Is Cycle: " + result);
76 if(result){ 77 Map<String,Object> resultMap = cycleLength(node1); 78 System.out.println("Cycle Length is: " + resultMap.get("length")); 79 System.out.println("Cycle Enter Node is: " + resultMap.get("enterNode")); 80 } 81 } 82 83 }

result:

p1: 3, p2: 7
p1: 7, p2: 6
p1: 2, p2: 1
p1: 6, p2: 6
========
Is Cycle: true
Cycle Length is: 4
Cycle Enter Node is: 2

 


 

Guess you like

Origin www.cnblogs.com/zldmy/p/11484877.html