简体   繁体   English

交换链表Java中的两个相邻节点

[英]Swapping two adjacent nodes in a linked list java

I have a very trivial problem, what should be a matter of just changing links. 我有一个非常琐碎的问题,应该只是更改链接。 I've read a few answers, and some show how to do this by swapping the data, and some give a vague explanation of the concept. 我已经阅读了一些答案,有些显示了如何通过交换数据来做到这一点,还有一些对此概念进行了模糊的解释。

Here's the method that seems to be running me in circles. 这似乎使我无所适从。 When I swap the target node to the previous node, the node is simply skipped. 当我将目标节点交换到上一个节点时,该节点将被跳过。 Then when I go back to reference the next node, I get stuck in an eternal loop. 然后,当我返回引用下一个节点时,我陷入了一个永恒的循环。 I need to know If I need another node traversing from the head, or if I can simply reference the new link. 我需要知道是否需要从头开始遍历另一个节点,或者是否可以简单地引用新链接。 I know I'm missing something quite obvious. 我知道我缺少明显的东西。

for (cursor = head; cursor != null; cursor = cursor.link) {
  if (target == cursor.data) {
    Target = cursor.link.getLink();
    next = cursor.getLink();
    prev = cursor;
    System.out.println(prev.getData()); // for testing
    System.out.println(next.getData());
    prev.setLink(Target); // Swaps the first link
    //Target.setLink(prev); // causes eternal loop
  }}
  return Target.getData();
}

This is my testing method, creating a list of 7 nodes, and printing to the screen. 这是我的测试方法,创建7个节点的列表,然后打印到屏幕上。

public static void main(String[] args) {
  SLL LL = new SLL(18, null);

  LL.add(4);
  LL.add(14);
  LL.add(8);
  LL.add(12);
  LL.add(2);
  LL.add(28);
  System.out.println(LL.toString());
  System.out.println(LL.swap(12));
  System.out.println(LL.toString());
}

This is the output I get: 这是我得到的输出:

{18, 28, 2, 12, 8, 14, 4} {18,28,2,12,12,8,14,4}

12 12

8 8

14 14

{18, 28, 2, 12, 14, 4} {18,28,2,12,12,14 4}

Desired output would be: 所需的输出为:

{18, 28, 2, 12, 8, 14, 4} {18,28,2,12,12,8,14,4}

{18, 28, 2, 8, 12, 14, 4} {18,28,2,8,8,12,14,4}

It looks like you're trying to swap a specific node (determined by the value of the node) with the node after it?. 看起来您正在尝试将特定节点(由节点的值确定)与该节点之后的节点交换?

The easiest solution to the problem you presented is to just swap the values, instead of trying to swap the nodes. 解决您出现的问题的最简单方法是仅交换值,而不是尝试交换节点。

Something like this should work since you're dealing with primitives 像这样的事情应该起作用,因为您正在处理基本体

if(currNode.value() == targetValue) {
   Node nextNode = currentNode.next();
   currentNode.setValue(nextNode.getValue()); //set the current node's value to the next node's value
   nextNode.setValue(targetValue);
}

Just be sure to handle the case where your target value is in the last node in the list. 只要确保处理您的目标值在列表的最后一个节点中。

Edit- since for some reason you want to change the links instead, the general logic is- 编辑-由于您出于某种原因想要更改链接,因此一般逻辑是-

  • keep track of 3 nodes: prev, current, next 跟踪3个节点:上一个,当前,下一个
  • set prev.link = next 设置prev.link =下一个
  • set current.link = next.link 设置current.link = next.link
  • set next.link = current 设置next.link =当前

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

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