Redian新闻
>
array 转换成 linkedlist, 在线等, 挺急的--help is still nee
avatar
array 转换成 linkedlist, 在线等, 挺急的--help is still nee# JobHunting - 待字闺中
H*e
1
希望static node fromArray(int[] a) {...}中的head或者tail只用到一个, 应该如何
update codes? Expect working codes in visual studio with only head or tail,
which mean eliminate the head or tail. Just 1 variable, no constructor. :)
hint:
You do not need double-linked list. Just think, why do we have head and tail
variables. We do not do anything with head except initial assignment.
class node
{
public int value;
public node next = null;
static node fromArray(int[] a)
{
if (a.Count() == 0) return null;
else
{
node head = new node();
node tail = head;
for (int i = 0; i < a.Count(); i++)
{
tail.value = a[i];
if (i != a.Count() - 1)
{
tail.next = new node();
tail = tail.next;
}
}
return head;
}
}
static void Main(string[] args)
{
int[] c = new int[] { 11, 2, 3 };
node my_list = node.fromArray(c);
while (my_list != null)
{
Console.WriteLine(my_list.value);
my_list = my_list.next;
}
Console.ReadLine();
}
}
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。