在链表java中切换节点时出现问题

在链表java中切换节点时出现问题,java,linked-list,logic,Java,Linked List,Logic,我试图在java中切换链表中的两个节点,但遇到了一些问题。我的排序算法工作正常,因为我通过切换节点的内容进行了检查,但是当尝试切换节点本身时,我遇到了问题 以下是我的节点开关代码: Node tmp = current; tmp.next = current.next.next; Node tmp2 = current.next; tmp2.next = curren

我试图在java中切换链表中的两个节点,但遇到了一些问题。我的排序算法工作正常,因为我通过切换节点的内容进行了检查,但是当尝试切换节点本身时,我遇到了问题

以下是我的节点开关代码:

                Node tmp = current;
                tmp.next = current.next.next;

                Node tmp2 = current.next;
                tmp2.next = current;


                current.next = tmp;
                current = tmp2;
然而,有了这段代码,我的循环一直在循环,所以我确信我的切换逻辑有问题。如果有人能帮我解决这个问题,我将不胜感激


**澄清:我的目标是切换电流和电流。下一步

谢谢大家!

Node tmp = current;
tmp.next = current.next.next;
上面的代码首先获取当前节点并使
tmp
指向它。然后它更改
tmp
中的下一个。问题是,这也将改变当前的
,因为它们指向相同的东西

你想要的是:

Node tmp = current.next.next;
current.next.next = current;
current.next = tmp;

试试这个代码。我使用的命名约定与您略有不同,但代码在
current
current.next
处交换节点

Node head = current.next; // the node to become the start of the list (may be null)

if (head != null) {
  // swap current node's position
  current.next = head.next;
  head.next = current;

  // update the node current is pointing to
  current = head;
}
从视觉上看,这就是正在发生的事情:


Current->B->List的其余部分…
将变成
B->Current->List的其余部分…

您就快到了。当我在大学里做这件事的时候,为了交换2个节点,你想抓住你想交换的第一个节点之前的节点。例如。如果您有

-->currNode->node\u A->node\u B->node\u C-->

在这里,我们希望将
节点A
节点B
交换,因此我们在
currenode
停止,因为我们需要设置
currenode。下一步
节点B

接下来,我们有以下内容(我假设它将被传递到某个方法中):

现在我们只需要设定正确的事情

tmp.setNext(B);  //Now we have ----> tmp -> B
B.setNext(A);   //Now we have  ----> tmp -> B -> A
A.setNext(C);   //Now we have ----> tmp -> B -> A -> C --->
现在,如果
node_A
恰好是第一个节点,或者
node_B
恰好是最后一个节点,那么其中一定有一些额外的条件,您可能需要记住。
如果
node\u A
恰好是链接列表中的第一个节点,则可以进行如下检查:

public void swap (NodeStructure nodeStructure, Node Node_A, Node Node_B){
    Node A = Node_A;
    Node B = Node_b;
    if(nodeStructure.head == A){
       //Node A is the first Node, so we need to handle it in a special way.
       Node tmp = Node_A;
       nodeStructure.setHead(B); //Now we have -> B
       B.setNext(tmp);           //Now we have -> B -> A
       A.setNext(C);             //Now we have -> B -> A -> C ------>
    }

    //or in the case of the tail 

    if(nodeStructure.tail == B){
       //Node B is the last Node, in this case, we don't need node_C
       /*Iterate through nodeStructure until you reach node before A and  
         assign this to tmp.*/
       nodeStructure.setTail(A); //Let's set the tail first
       tmp.setNext(B);  //Now we have ----> tmp -> B
       B.setNext(A);   //Now we have  ----> tmp -> B -> A
    }

     /* Depeding on how you decide to implement, you might also have a third 
        condition checking if Node_A is the head and Node_B is tail.*/

    //Handle condition where there's no special cases.
}

好吧,我终于明白了

我完全弄错了。这只是一个重新链接的问题。以下是我的解决方案:

                Node temp = current;
                Node prevNext = prev.next;
                Node currentNext = current.next;
                Node currentNextNext = current.next.next;


                current.next.next = temp;
                current.next = currentNextNext;
                prev.next = currentNext;

希望它能帮助那些像我一样陷入困境的人

你打算切换哪两个节点?我的目标是切换电流和电流。下一次我在大学时这样做时,我确保在我想交换的第一个
节点之前抓住
节点。此时,它只是正确链接接下来的3个
节点。当您设置
tmp.next=current.next.next
时,您也在设置
current.next=current.next.next
,因为
tmp
也在引用
current
。这意味着您丢失了对
current.next的引用。您认为这两行的作用是什么:
Node tmp=current;current.next=tmp
?执行这些行之后,
current.next
指向何处?由于某种原因,当我尝试您的建议时,我丢失了大约一半的节点。在何处设置当前值?在某个点上,节点c=curr.next.next.next出于某种原因引发空指针异常,因此它不是working@user3105072,您必须记住,如果您尝试交换的节点是第一个和/或最后一个节点,则会出现特殊情况。如果这是抛出异常,则表示节点_B是最后一个节点。在这一点上,你不需要node_C.im仍然无法将其整合在一起。。你能用我可以插入的代码编辑你的答案来改变current和current.next吗?@user3105072你能跟踪节点结构的头部和尾部吗?
                Node temp = current;
                Node prevNext = prev.next;
                Node currentNext = current.next;
                Node currentNextNext = current.next.next;


                current.next.next = temp;
                current.next = currentNextNext;
                prev.next = currentNext;