【问题标题】:Having problems switching nodes in a linked list java在链表java中切换节点时出现问题
【发布时间】:2016-05-03 00:05:04
【问题描述】:

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

以下是我的节点切换代码:

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

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


                current.next = tmp;
                current = tmp2;

但是,使用此代码,我的循环一直在循环,所以我确信我的切换逻辑存在问题。如果有人能帮我解决这个问题,我将不胜感激。

** 澄清一下:我的目标是切换 current 和 current.next。

谢谢!

【问题讨论】:

  • 你打算切换哪两个节点?
  • 我的目标是切换current和current.next
  • 当我回到大学时,我确保在我想交换的第一个 node 之前抓住了 node。此时它只是正确链接了下一个 3 nodes
  • 当您设置tmp.next = current.next.next 时,您也设置了current.next = current.next.next,因为tmp 也引用了current。这意味着您丢失了对 current.next 的引用。
  • 你认为这两行代码有什么作用:Node tmp = current; current.next = tmp?这些行执行后current.next指向哪里?

标签: java linked-list logic


【解决方案1】:

你快到了。回到我在大学做这个的时候,为了交换 2 个节点,你想要获取你想要交换的第一个节点之前的节点。例如。如果你有

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

在这里,我们希望将node_Anode_B 交换,因此我们在currNode 处停止,因为我们需要将currNode.next 设置为node_B

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

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

现在我们只需要设置正确的东西。

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_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.
}

【讨论】:

  • 在某些时候节点 c = curr.next.next.next 行由于某种原因抛出空指针异常,因此它不起作用
  • @user3105072,您必须记住,如果您尝试交换的节点是第一个和/或最后一个节点,则会出现特殊情况。如果抛出异常,则表示 node_B 是最后一个节点。此时您不需要node_C。
  • 我仍然无法将它们组合在一起。.. 你能用我如何插入代码来更改 current 和 current.next 来编辑你的答案吗?
  • @user3105072 是否跟踪节点结构的头尾?
【解决方案2】:
Node tmp = current;
tmp.next = current.next.next;

上面的代码首先获取当前节点并使tmp指向它。然后它改变了tmp 的下一个。问题是,这也会改变current,因为它们指向同一个东西。

你想要这样的东西:

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

【讨论】:

  • 由于某种原因,当我尝试您的建议时,我丢失了大约一半的节点。你在哪里设置电流?
【解决方案3】:

试试这个代码。我使用的命名约定与您略有不同,但代码是在 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;
}

在视觉上这是正在发生的事情:

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

【讨论】:

    【解决方案4】:

    好吧,我终于想通了!

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

                    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;
    

    希望它可以帮助任何可能像我一样被卡住的人!

    【讨论】:

      猜你喜欢
      • 2018-05-05
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 2019-12-30
      • 1970-01-01
      • 2016-06-17
      • 2012-02-14
      • 1970-01-01
      相关资源
      最近更新 更多