c# - 节点列表中所有 EVEN 值在开头,赔率在结尾
问题描述
那是高中问题类型,因此不允许使用“Heavy wepones”,也不允许与数组一起使用,问题是:假设我有所有节点列表,我应该将所有具有偶数值的节点放在列表的开头和我应该将所有具有奇数值的节点放在列表的末尾,即:
3,2,7,6,9,4
--->2,6,4,3,7,9
节点类:
public class Node
{
public int value;
public Node next;
public Node(int value)
{
this.value = value;
this.next = null;
}
public Node(int value, Node next)
{
this.value = value;
this.next = next;
}
public void SetNext( Node next)
{
this.next = next;
}
public void SetValue(int value)
{
this.value = value;
}
public Node GetNext()
{
return this.next;
}
public int GetValue()
{
return this.value;
}
}
主类:
Node n1 = new Node(3);
Node n2 = new Node(2);
Node n3 = new Node(1);
Node n4 = new Node(4);
Node n5 = new Node(9);
Node n6 = new Node(6);
n1.SetNext(n2);
n2.SetNext(n3);
n3.SetNext(n4);
n4.SetNext(n5);
n5.SetNext(n6);
Ex(n1); // Invoking the function
我已经开始了类似的事情:
private void Ex(Node list)
{
Node temp = list;
while (temp != null)
{
if (temp.GetValue() % 2 == 0)
{
}
}
}
解决方案
推荐阅读
- javascript - 使用变量输入创建嵌套数组/对象的 Javascript 函数
- node.js - 使用 pdf2json 将 pdf 解析为 json 时出现 TypeError
- fullcalendar - Fullcalendar V4:如何将从 ajax 接收到的 json 解析为事件列表
- jquery - 如何处理网关 API 和 Lambda 代理的错误?
- c++ - 检索函数对象的可调用运算符的所有重载的参数类型
- java - spring boot项目可以不用互联网搭建开发吗?
- python - 如何在每年重置的python中生成带有年份和序列号的task_id?
- java - 检查 BT 是否是带有 T 扩展可比性的 BST
爪哇 - winapi - DialogBox 未正确绘制对话框
- c++ - 将函数转换为成员函数