繁体   English   中英

如何在链表循环中查找节点数?

[英]how to find number of nodes in loop of linked list?

如何在链表循环中找到节点数?

例如

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

找到从C到H的循环中的节点数

根本问题是如何找到C点。我们可以使用传统的野兔和乌龟算法,但它不会在C点每次都遇到。

请参阅此处有关如何在链表中查找循环的更多解决方案。 添加节点计数非常简单。 (虽然乌龟和野兔可能是最好的一个)

如果你只是想找到答案,那就做龟龟来确定什么时候肯定有一个循环。 然后启动一个计数器,并计算您必须进行多少次迭代才能达到您第一次找到的点。 这可能不是最有效的,但它给出了正确的答案。

一些C ++代码:

#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

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

我不认为我会认为这是一个linkList。 LinkedLists通常以空指针或指向结束符号的指针结束。 即: start -> A -> B -> C -> end 我认为这将是一种特定的图形。

要查找图中的节点总数,我会这样做:

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;

1)flyod alogo找到循环2)当slow_ptr = fast_ptr时,找到循环中的节点数(k)

另外你也可以像这样去C: - 3)开始2 ptr,一个从头开始,另一个从头+ k开始。 4)你将在Loop(C)开始时见面

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM