How to find the number of nodes in a linked list loop?

How to find the number of nodes in a linked list loop?

eg,

A ----> B ----> C -----> D -----> E
                Λ                 |
                |                 |
                |                 V
                H <----- G <----- F 

Find the number of nodes in a cycle from C to H

The main problem is how to find point C. We can use the traditional hare and tortoise algo, but it does not occur every time at point C.

+3
source share
5 answers

I don’t think I think this is a linked list. LinkedLists usually end with a null pointer or a pointer pointing to a trailing character. I.e start -> A -> B -> C -> end. : . I think it will be a certain kind of graph.

To find the total number of nodes in a graph, I would do the following:

List visited;
List toVisit;

toVisit.add(A);                         // add the first Node
while(toVisit is not empty){
  Node current = visited.remove();
  Array <Node> links = current.getLinks();
  for(int i=0; i<links.size(); i++){
    if(!visited.contains(links[i])){    // if the link has NOT already been visited add it to the toVisit List
      toVisit.add(links[i]);
    }        
  visited.add(current);                 // mark current as visited
  }
}
return visited.size();                  // to get the number of nodes in the graph

, ( ...):

A ---> ... ---> C -----> D -----> E
                Λ                 |
                |                 |
                |                 V
                ... <----- G <--- F 

:

List visited;

Node current = firstNode;
while(!visited.contains(firstNode)){
  Node next = current.getNext();      
  visited.add(current);                       // mark current as visited
  current=next;
}
// our ending condition is when we have found the same node again.  
int currentIndex = visited.indexOf(current);
int size = visited.size();
int sizeOfLoop = size - currentIndex;
return sizeOfLoop;
0

, . . ( "" "", , )

+4

, -, , . , , , . , .

++-:

#include <iostream>

struct node
{
  node(node* next)
    : next(next)
  { }

  node* next;
};

int main(int argc, char* argv[])
{
  node h(NULL), g(&h), f(&g), e(&f), d(&e), c(&d), b(&c), a(&b);
  h.next = &c;

  node* tortoise = &a;
  node* hare = &b;

  while(tortoise != hare)
  {
    tortoise = tortoise->next;
    hare = hare->next->next;
  }

  int count = 1;
  tortoise = tortoise->next;

  while(tortoise != hare)
  {
    ++count;
    tortoise = tortoise->next;
  }

  std::cout << "Size of cycle: " << count << "\n";

  return 0;
}

, , , , , , . :

http://en.wikipedia.org/wiki/Cycle_detection

+2
source
List visited;
List toVisit;

toVisit.add(A);                         // add the first Node
while(toVisit is not empty){
  Node current = visited.remove();
  Array <Node> links = current.getLinks();
  for(int i=0; i<links.size(); i++){
    if(!visited.contains(links[i])){    // if the link has NOT already been visited add it to the toVisit List
      toVisit.add(links[i]);
    }        
  visited.add(current);                 // mark current as visited
  }
}
return visited.size();                  // to get the number of nodes in the graph
+1
source

1) flyod alogo find the loop 2) when slow_ptr = fast_ptr, find the number of nodes in the loop (k)

Alternatively, you can also go to C: 3) run 2 ptr, one from the head and the other from the head + k. 4) You will meet at startup Loop (C)

0
source

Source: https://habr.com/ru/post/1769107/


All Articles