Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
递归地将Java中的LinkedList拆分为奇数和偶数列表_Java_Data Structures_Linked List_Nodes - Fatal编程技术网

递归地将Java中的LinkedList拆分为奇数和偶数列表

递归地将Java中的LinkedList拆分为奇数和偶数列表,java,data-structures,linked-list,nodes,Java,Data Structures,Linked List,Nodes,我试图解决一个问题,我得到一个带有数据整数的LinkedList,任务是将列表拆分为两个单独的列表,一个是所有偶数位置的列表,另一个是所有奇数位置的列表 输入:0112304-5 ListNode类: public class ListNode { public int data; public ListNode next; public ListNode(int _data) { data = _data; next = nul

我试图解决一个问题,我得到一个带有数据整数的LinkedList,任务是将列表拆分为两个单独的列表,一个是所有偶数位置的列表,另一个是所有奇数位置的列表

输入:0112304-5

ListNode类:

public class ListNode
{
    public int data;
    public ListNode next;

    public ListNode(int _data)
    {
        data = _data;
        next = null;
    }
}

驱动程序方法:

    public static void main(String [] args) {
        MyList L = new MyList();
        ListNode head = L.getHead();        // Get the head node of the linked list.
        System.out.print("INPUT: ");
        print(head);
        ListNode [] R = split(head);        // Split into two list. The first list contains all elements in odd positions, the second contains all elements at even positions.
        System.out.println("Printing the list with odd positions");
        print(R[0]);
        System.out.println("Printing the list with even positions");
        print(R[1]);
    }
我正在努力思考一种方法来做到这一点,我没有得到任何辅助方法来添加节点,这里有一些我已经尝试过但不能正常工作的方法

private static ListNode[] split(ListNode L)
    {
        ListNode[] ret = new ListNode[2];
        
        for (ListNode cur = L; cur != null; cur = cur.next)
        {
            if ((cur.data % 2) == 0)
            {
                ret[1] = cur;
                ret[1].data = cur.data;
                System.out.print("CURRENT EVEN LIST IS ");
                print(ret[1]);
            }
            else
            {
                ret[0] = cur;
                ret[0].data = cur.data;
                System.out.print("CURRENT ODD LIST IS ");
                print(ret[0]);
            }
        }
        return ret;
    }
这是我得到的结果:

INPUT: 0 1 2 3 0 -4 -5 
CURRENT EVEN LIST IS 0 1 2 3 0 -4 -5 
CURRENT ODD LIST IS 1 2 3 0 -4 -5 
CURRENT EVEN LIST IS 2 3 0 -4 -5 
CURRENT ODD LIST IS 3 0 -4 -5 
CURRENT EVEN LIST IS 0 -4 -5 
CURRENT EVEN LIST IS -4 -5 
CURRENT ODD LIST IS -5 
Printing the list with odd positions
-5 
Printing the list with even positions
-4 -5 
我希望有人能引导我走上正确的道路,我知道我的尝试是错误的,因为它总是复制整个列表,但我知道我错过了一些东西。有递归的方法吗


谢谢

在单链接列表中,头节点隐式表示整个列表,第二个节点表示自身和列表的其余部分

执行此操作时,
ret[1]=cur
当cur是头节点时,您只是将该索引设置为整个列表,并且在for循环的下一次迭代中,当cur是第二个节点时
ret[1]=cur
将ret[1]设置为减去上一个节点的整个列表

您需要更改原始列表中的连接。试着把它画出来

原始列表有如下连接:

(0)->(1)->(2)->(3)->(0)->(4)->(5)

你想要这个:

(0)->(2)->(0)->(-5)

(1) ->(3)->(-4)

将使用节点定义中的下一个字段设置连接/链接。您必须断开连接,并设置新的连接


例如,第一个(0)的下一个字段将是(1)的原始下一个字段,代码如下:
cur.next=cur.next.next

在单链接列表中,头节点隐式表示整个列表,第二个节点表示自身和列表的其余部分

执行此操作时,
ret[1]=cur
当cur是头节点时,您只是将该索引设置为整个列表,并且在for循环的下一次迭代中,当cur是第二个节点时
ret[1]=cur
将ret[1]设置为减去上一个节点的整个列表

您需要更改原始列表中的连接。试着把它画出来

原始列表有如下连接:

(0)->(1)->(2)->(3)->(0)->(4)->(5)

你想要这个:

(0)->(2)->(0)->(-5)

(1) ->(3)->(-4)

将使用节点定义中的下一个字段设置连接/链接。您必须断开连接,并设置新的连接


例如,第一个(0)的下一个字段将是(1)的原始下一个字段,代码如下:
cur.next=cur.next.next

这里是一个递归实现

    private void split(Predicate<Integer> p, ListNode n, Pair<ListNode> pair) {
        if (null == n) {
            return;
        }

        if (p.test(n.data)) {
            pair.left = pair.left.next = n;
        } else {
            pair.right = pair.right.next = n;
        }

        ListNode nn = n.next;
        n.next = null;
        split(p, nn, pair);
    }
要开始递归拆分,您需要使用伪根对象创建
,以便在递归步骤中消除对第一个条目的空检查。完成后,只需剥下收割台,然后将剩余部分退回即可

    Pair<ListNode> split(Predicate<Integer> p) {
        ListNode leftRoot = new ListNode(0);
        ListNode rightRoot = new ListNode(0);
        Pair<ListNode> pair = new Pair<>(leftRoot, rightRoot);
        split(p, this, pair);
        return new Pair<>(leftRoot.next, rightRoot.next);
    }
您将获得以下输出:

0 -> 2 -> 0 -> -4 -> 
1 -> 3 -> -5 -> 

这里是一个递归实现

    private void split(Predicate<Integer> p, ListNode n, Pair<ListNode> pair) {
        if (null == n) {
            return;
        }

        if (p.test(n.data)) {
            pair.left = pair.left.next = n;
        } else {
            pair.right = pair.right.next = n;
        }

        ListNode nn = n.next;
        n.next = null;
        split(p, nn, pair);
    }
要开始递归拆分,您需要使用伪根对象创建
,以便在递归步骤中消除对第一个条目的空检查。完成后,只需剥下收割台,然后将剩余部分退回即可

    Pair<ListNode> split(Predicate<Integer> p) {
        ListNode leftRoot = new ListNode(0);
        ListNode rightRoot = new ListNode(0);
        Pair<ListNode> pair = new Pair<>(leftRoot, rightRoot);
        split(p, this, pair);
        return new Pair<>(leftRoot.next, rightRoot.next);
    }
您将获得以下输出:

0 -> 2 -> 0 -> -4 -> 
1 -> 3 -> -5 -> 

你想要递归,但是你展示的方法是迭代的是,我正在寻找一个正确的方向,关于如何递归地处理这个问题,或者如果可能的话,帮助纠正我的迭代方法。你想要递归,但是你展示的方法是迭代的是,我正在寻找一个正确的方向,如何递归地处理这个问题,或者如果可能的话,帮助纠正我的迭代方法。我认为您的子列表的最后元素是混合的。假设输入为0 1 2 3 0-4-5,我所描述的不正确吗?因为偶数位置是0,2,4,6,奇数位置是1,3,5。对不起,你可能是对的。我解释为偶数值,而不是偶数位置。然而,条件
cur.data%2
表示数据,但谁知道没有预期的输出。我对此也感到困惑,但最终注意到他的驱动程序方法中有一条注释。但你是对的,他在if语句中的表达确实表明了这一点。尽管如此,一个预期的输出绝对可以解决这一困惑。我认为您的子列表的最后一个元素是混合的。假设输入为0 1 2 3 0-4-5,我所描述的不是正确的吗?因为偶数位置是0,2,4,6,奇数位置是1,3,5。对不起,你可能是对的。我解释为偶数值,而不是偶数位置。然而,条件
cur.data%2
表示数据,但谁知道没有预期的输出。我对此也感到困惑,但最终注意到他的驱动程序方法中有一条注释。但你是对的,他在if语句中的表达确实表明了这一点。尽管如此,一个预期的输出绝对可以解决这个困惑。