【发布时间】:2018-01-22 12:08:52
【问题描述】:
我正在从 C# 中的链表数据结构中解决一个程序,我需要检查给定的链表是 NULL 终止还是以循环结束。 我想用不同的测试用例检查它,但不能将循环链表作为输入传递。
如何将循环链表作为输入传递?
Problem from hackerrank 会给你一个想法,我想要实现什么?
这是我实现链表的代码,显示在image
private static LinkedList<int> InitializeLinkedList ()
{
LinkedList<int> linkedList = new LinkedList<int>();
LinkedListNode<int> item1 = new LinkedListNode<int>(1);
LinkedListNode<int> item2 = new LinkedListNode<int>(2);
LinkedListNode<int> item3 = new LinkedListNode<int>(3);
LinkedListNode<int> item4 = new LinkedListNode<int>(4);
LinkedListNode<int> item5 = new LinkedListNode<int>(5);
LinkedListNode<int> item6 = new LinkedListNode<int>(6);
linkedList.AddLast(item1);
linkedList.AddLast(item2);
linkedList.AddLast(item3);
linkedList.AddLast(item4);
linkedList.AddLast(item5);
linkedList.AddAfter(item3, item6);
return linkedList;
}
【问题讨论】:
-
不确定是什么问题。 Hackerrank 已经有一个在线验证工具,它会调用你提供的代码,你具体要问什么?
-
如果您正在尝试编写类似的东西并想要构建测试用例,您将不得不向我们展示一些代码。你说你不能将循环链表作为输入传递?为什么不?编译器会阻止你吗?您是否很难弄清楚要写什么?好奇的人想知道。
-
您更新的问题代码与图片不匹配。
AddAfter将在 item3 和 item 4 之间插入 item。使用内置的LinkedList<>创建循环链表是不可能的。 -
“无法将循环链表作为输入传递” - 为什么不呢,这是代码问题吗?如果您发布的代码与您的问题相关,我不知道如何。
标签: c# data-structures linked-list cycle-detection