首页 > 解决方案 > 节点列表中所有 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)
                {

                }
            }          
        }

标签: c#linked-list

解决方案


推荐阅读