使用 C# 在 LinkedList 中进行循环检测

发布于 2024-12-19 12:32:48 字数 620 浏览 2 评论 0原文

在面试问题中,“实现一种检测循环存在的算法。”。例如链表有环,如:

0--->1---->2---->3---->4---->5---->6
                 ▲                 |
                 |                 ▼
                11<—-22<—-12<—-9<—-8

利用Floyd的环检测,可以通过使用fast & 来解决这个问题。慢指针。那么我应该尝试比较

a. 链接的节点值,即

if (fast.data == slow.data) 
    break;

快和慢的类型为Link

class Link
{
    int IData {get; set;}
    Link Next {get; set;}
}

OR

b。 它们是否指向相同的引用,即if(快==慢)

谢谢。

In the interview question, "Implement an algorithm which detects for presence of the loop.". For example, the linked list has a loop, like:

0--->1---->2---->3---->4---->5---->6
                 ▲                 |
                 |                 ▼
                11<—-22<—-12<—-9<—-8

Using Floyd's cycle detection, this problem can be solved by using a fast & slow pointer. So should I try comparing

a. Link's node values, i.e.

if (fast.data == slow.data) 
    break;

where fast and slow are of type Link

class Link
{
    int IData {get; set;}
    Link Next {get; set;}
}

OR

b. Are they pointing to same reference, i.e. if (fast == slow)

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

度的依靠╰つ 2024-12-26 12:32:49

希望这会有所帮助......这可能很天真,但它有效......

using System;

namespace CSharpTestTemplates
{
class LinkedList
{
    Node Head;

    public class Node
    {
        public int value;
        public Node NextNode;

        public Node(int value)
        {
            this.value = value;
        }
    }

    public LinkedList(Node head)
    {
        this.Head = head;
    }



    public Boolean hasLoop()
    {
        Node tempNode = Head;
        Node tempNode1 = Head.NextNode;
        while(tempNode!=null && tempNode1!=null){
            if(tempNode.Equals(tempNode1)){
                return true;
            }

            if ((tempNode1.NextNode != null) && (tempNode.NextNode != null))
            {
                tempNode1 = tempNode1.NextNode.NextNode;
                tempNode = tempNode.NextNode;
            }
            else
            {
                return false;
            }
        }

        return false;
    }

    public static void Main()
    {
        Node head = new Node(1);
        LinkedList ll = new LinkedList(head);

        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        Node node5 = new Node(5);
        Node node6 = new Node(6);

        head.NextNode = node2;
        node2.NextNode = node3;
        node3.NextNode = node4;
        node4.NextNode = node5;
        node5.NextNode = node6;
        node6.NextNode = null;

        Console.WriteLine(ll.hasLoop());
        Console.Read();
    }
   }
 }

Hope this helps... It might be naive but it works...

using System;

namespace CSharpTestTemplates
{
class LinkedList
{
    Node Head;

    public class Node
    {
        public int value;
        public Node NextNode;

        public Node(int value)
        {
            this.value = value;
        }
    }

    public LinkedList(Node head)
    {
        this.Head = head;
    }



    public Boolean hasLoop()
    {
        Node tempNode = Head;
        Node tempNode1 = Head.NextNode;
        while(tempNode!=null && tempNode1!=null){
            if(tempNode.Equals(tempNode1)){
                return true;
            }

            if ((tempNode1.NextNode != null) && (tempNode.NextNode != null))
            {
                tempNode1 = tempNode1.NextNode.NextNode;
                tempNode = tempNode.NextNode;
            }
            else
            {
                return false;
            }
        }

        return false;
    }

    public static void Main()
    {
        Node head = new Node(1);
        LinkedList ll = new LinkedList(head);

        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        Node node5 = new Node(5);
        Node node6 = new Node(6);

        head.NextNode = node2;
        node2.NextNode = node3;
        node3.NextNode = node4;
        node4.NextNode = node5;
        node5.NextNode = node6;
        node6.NextNode = null;

        Console.WriteLine(ll.hasLoop());
        Console.Read();
    }
   }
 }
素食主义者 2024-12-26 12:32:48

您应该只比较节点本身。毕竟,有一个包含重复数据的链表是合理的,但实际上没有循环。

我也将它们称为节点而不是链接。链接只是从一个节点到下一个或上一个节点的引用 - 特别是,没有与链接关联的数据,仅与节点关联。

You should only be comparing the nodes themselves. After all, it's reasonable to have a linked list with repeated data in, without it actually having a cycle.

I would call them nodes rather than links too. A link is simply the reference from one node to the next or previous one - in particular, there's no data associated with a link, only with a node.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文