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

时间:2010-10-12 10:22:37

标签: algorithm linked-list

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

例如

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

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

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

5 个答案:

答案 0 :(得分:4)

有关如何在链接列表中查找循环的详细信息,请参阅here。添加节点计数非常简单。 (虽然乌龟和野兔可能是最好的一个)

答案 1 :(得分:2)

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

一些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

答案 2 :(得分:1)

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

答案 3 :(得分:0)

我认为我不认为这是一个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;

答案 4 :(得分:0)

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

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