简体   繁体   English

如何分别交错这两个链表?

[英]How do I interleave these two linked lists separately?

Im trying to interleave two lists such that:我试图交错两个列表,使得:

list1 starts as {a1,a2,a3,a4,a5} list2 starts as {b1,b2,b3,b4,b5} list1 从 {a1,a2,a3,a4,a5} 开始 list2 从 {b1,b2,b3,b4,b5} 开始

And I want them to be {a1,b2,a3,b4,a5} {b1,a2,b3,a4,b5}我希望它们是 {a1,b2,a3,b4,a5} {b1,a2,b3,a4,b5}

This is my code:这是我的代码:

public void interleave(A3LinkedList other) {
  A3Node thisCurrent = this.head;
  A3Node otherCurrent = other.head;
    int count = 1;
    while(count <= this.length){
        System.out.println("List1 current at loop "+ count + ": " + thisCurrent); // just to debug
        System.out.println("List2 current at loop "+ count + ": " + otherCurrent);

        A3Node tempThis = thisCurrent;
        A3Node tempOther = otherCurrent;

        //if(count%2 == 0){ //if count is even, aka every second object in list
            thisCurrent = tempOther; // makes thisCurrent same as otherCurrent
            otherCurrent = tempThis; // makes otherCurrent same as temp saved
        //}


        thisCurrent = thisCurrent.next;
        otherCurrent = otherCurrent.next;
        count ++;
    }
}

The output from the println methods in the while loop show me that the nodes are swapping correctly, but the end result in another tester program shows me that the lists arent changing at all. while 循环中 println 方法的输出显示节点交换正确,但另一个测试程序中的最终结果显示列表根本没有改变。

Output from inside the while loop: while 循环内部的输出:

List1 current at loop 1: A
List2 current at loop 1: L
List1 current at loop 2: M
List2 current at loop 2: B
List1 current at loop 3: C
List2 current at loop 3: N
List1 current at loop 4: O
List2 current at loop 4: D
List1 current at loop 5: E
List2 current at loop 5: P
List1 current at loop 6: Q
List2 current at loop 6: F
List1 current at loop 7: G
List2 current at loop 7: R

Output from tester: {ABCDEFG} {LMNOPQR} Failed test: testInterleave at line 203 Failed test: testInterleave at line 204测试器的输出:{ABCDEFG} {LMNOPQR} 测试失败:第 203 行的 testInterleave 失败的测试:第 204 行的 testInterleave

As one can see, the tester output isn't what it should be, it isn't passing the test.正如你所看到的,测试仪的输出不是它应该的样子,它没有通过测试。 Why?为什么?

Your code isn't working because you're creating local object references in the method and only reassigning those local references.您的代码无法正常工作,因为您正在方法中创建本地对象引用,并且只重新分配这些本地引用。 You need to reassign the references' members (ie A3Node.next ) and what they point to.您需要重新分配引用的成员(即A3Node.next )及其指向的内容。 Like the following:像下面这样:

public void interleave(A3LinkedList other) {
        A3Node thisCurrent = this.head;
        A3Node otherCurrent = other.head;
        
        int count = 1;
        while(count <= this.length){
            // don't want to lose reference to other's next
            A3Node otherNext = otherCurrent.next;
            
            // now we change the references that next are pointing to
            
            // make otherCurrent.next point to thisCurrent.next
            otherCurrent.next = thisCurrent.next;
            // make thisCurrent.next point to otherNext.next (old otherCurrent.next)
            thisCurrent.next = otherNext;
            
            thisCurrent = thisCurrent.next;
            otherCurrent = otherCurrent.next;
            count += 1;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM