简体   繁体   English

在链表java中切换节点时遇到问题

[英]Having problems switching nodes in a linked list java

I am attempting to switch two nodes in a linked list in java and am having some issues.我试图在 java 中的链表中切换两个节点,但遇到了一些问题。 My sorting algorithm works properly as I checked it by switching the contents of the node, but when attempting to switch the nodes themselves I am getting issues.当我通过切换节点的内容来检查它时,我的排序算法工作正常,但是当我尝试切换节点本身时,我遇到了问题。

The following is my node switch code:以下是我的节点切换代码:

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

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


                current.next = tmp;
                current = tmp2;

With this code however, my loop keeps looping so I am sure there is a problem with my switching logic.但是,使用此代码,我的循环不断循环,因此我确定我的切换逻辑存在问题。 If anyone could help me figure this out I would greatly appreciate it.如果有人能帮我解决这个问题,我将不胜感激。

** to clarify: I am aiming to switch current and current.next. ** 澄清:我的目标是切换 current 和 current.next。

Thank you!谢谢!

You're almost there.您快到了。 Back when I did this in college, in order to swap 2 nodes you want to grab the node that's before the first node you wish to swap.回到我在大学里做的时候,为了交换 2 个节点,你想要获取你想要交换的第一个节点之前的节点。 For instance.例如。 If you have如果你有

----> currNode -> node_A -> node_B -> node_C ------>

Here we wish to swap node_A with node_B so we stop at currNode since we need to set currNode.next to node_B .在这里,我们希望将node_Anode_B交换,因此我们在currNode停止,因为我们需要将currNode.next设置为node_B

Following this, we have the following (i'm assuming it'll get passed into some method):在此之后,我们有以下内容(我假设它会被传递到某个方法中):

Node tmp = curr;
Node A = curr.next;
Node B = curr.next.next;
Node C = curr.next.next.next;

Now we just need to set the right things.现在我们只需要设置正确的东西。

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 --->

Now if node_A happens to be the first node or if node_B happens to be the last node then there's gotta be some extra conditions in there you might want to keep in mind.现在,如果node_A恰好是第一个节点,或者node_B恰好是最后一个节点,那么您可能需要记住一些额外的条件。
You can tell if node_A happens to be the first node in your linked list, You can have some for of check like:您可以判断node_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 tmp = current;
tmp.next = current.next.next;

The code above first gets the current node and makes tmp point to it.上面的代码首先获取当前节点并使tmp指向它。 Then it changes the next of tmp .然后它更改tmp的下一个。 The problem is, that also will change current , since they point to the same thing.问题是,这也会改变current ,因为它们指向同一件事。

You want something like:你想要这样的东西:

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

Try this code.试试这个代码。 I'm using slightly different naming conventions to you but the code is swapping the nodes at current and current.next .我对您使用的命名约定略有不同,但代码正在交换currentcurrent.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;
}

Visually this is what's happening:从视觉上看,这就是正在发生的事情:

Current -> B -> Rest of List... will become B -> Current -> Rest of List... Current -> B -> Rest of List...将变为B -> Current -> Rest of List...

Ok I finally figured it out!好吧我终于想通了!

I was going at it all wrong.It was simply a problem of re-linking.我完全错了。这只是重新链接的问题。 The following was my solution:以下是我的解决方案:

                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;

Hope it helps anyone who might be stuck like I was!希望它可以帮助任何可能像我一样陷入困境的人!

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

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