如何将这两个链表分开交错连接?

huangapple go评论67阅读模式
英文:

How do I interleave these two linked lists separately?

问题

以下是翻译的内容:

我正在尝试交错两个列表,使其如下:

list1初始为 {a1,a2,a3,a4,a5}
list2初始为 {b1,b2,b3,b4,b5}

我希望它们变成:
{a1,b2,a3,b4,a5}
{b1,a2,b3,a4,b5}

这是我的代码:

public void interleave(A3LinkedList other) {
    A3Node thisCurrent = this.head;
    A3Node otherCurrent = other.head;
    int count = 1;
    while(count <= this.length){
        System.out.println("循环中List1当前位置:" + count + ": " + thisCurrent); // 仅用于调试
        System.out.println("循环中List2当前位置:" + count + ": " + otherCurrent);

        A3Node tempThis = thisCurrent;
        A3Node tempOther = otherCurrent;

        //if(count%2 == 0){ // 如果count是偶数,即列表中的每个第二个对象
            thisCurrent = tempOther; // 使thisCurrent与otherCurrent相同
            otherCurrent = tempThis; // 使otherCurrent与之前保存的temp相同
        //}

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

在while循环中,println方法的输出显示节点交换是正确的,但在另一个测试程序中的最终结果表明列表根本没有改变。

循环内的输出:

循环中List1当前位置 1: A
循环中List2当前位置 1: L
循环中List1当前位置 2: M
循环中List2当前位置 2: B
循环中List1当前位置 3: C
循环中List2当前位置 3: N
循环中List1当前位置 4: O
循环中List2当前位置 4: D
循环中List1当前位置 5: E
循环中List2当前位置 5: P
循环中List1当前位置 6: Q
循环中List2当前位置 6: F
循环中List1当前位置 7: G
循环中List2当前位置 7: R

来自测试器的输出:

{ABCDEFG}
{LMNOPQR}
测试失败:第203行的testInterleave
测试失败:第204行的testInterleave

正如你可以看到的,测试器的输出并不是预期的输出,测试没有通过。为什么呢?

英文:

Im trying to interleave two lists such that:

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

And I want them to be
{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 &lt;= this.length){
		System.out.println(&quot;List1 current at loop &quot;+ count + &quot;: &quot; + thisCurrent); // just to debug
		System.out.println(&quot;List2 current at loop &quot;+ count + &quot;: &quot; + 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.

Output from inside the while loop:

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

As one can see, the tester output isn't what it should be, it isn't passing the test.
Why?

答案1

得分: 0

你的代码无法正常工作,因为你在方法中创建了局部对象引用,而只是重新赋值了这些局部引用。你需要重新分配引用的成员(例如 A3Node.next)及其指向的内容。就像以下这样:

public void interleave(A3LinkedList other) {
    A3Node thisCurrent = this.head;
    A3Node otherCurrent = other.head;
    
    int count = 1;
    while(count <= this.length){
        // 不希望丢失对 other 下一个节点的引用
        A3Node otherNext = otherCurrent.next;
        
        // 现在我们改变 next 引用的指向
        
        // 使 otherCurrent.next 指向 thisCurrent.next
        otherCurrent.next = thisCurrent.next;
        // 使 thisCurrent.next 指向 otherNext.next(旧的 otherCurrent.next)
        thisCurrent.next = otherNext;
        
        thisCurrent = thisCurrent.next;
        otherCurrent = otherCurrent.next;
        count += 1;
    }
}
英文:

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 (i.e. A3Node.next) and what they point to. Like the following:

public void interleave(A3LinkedList other) {
		A3Node thisCurrent = this.head;
		A3Node otherCurrent = other.head;
		
		int count = 1;
		while(count &lt;= this.length){
            // don&#39;t want to lose reference to other&#39;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;
		}
	}

huangapple
  • 本文由 发表于 2020年10月13日 10:41:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64327719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定