【问题标题】:How do I swap two nodes with their content in a singly linked list?如何在单链表中交换两个节点及其内容?
【发布时间】:2019-01-28 19:33:38
【问题描述】:

我用谷歌搜索了这个,但他们都在谈论“交换节点而不交换数据”。

我自己尝试写了一个交换节点方法:

public void swapNodes(int num1, int num2) {

    if(num1 == num2) {
        return;
    }

    Node currentNum1 = head;
    Node currentNum2 = head;
    Node waitForSwap1 = null;
    Node waitForSwap2 = null;
    while (currentNum1 != null) {
        if (currentNum1.data == num1) {
            waitForSwap1 = currentNum1;
            System.out.println();
            System.out.println("waitForSwap 1");
            System.out.println(waitForSwap1.data);

        }
        currentNum1 = currentNum1.next;
    }

    while (currentNum2 != null) {
        if (currentNum2.data == num2) {
            waitForSwap2 = currentNum2;
            System.out.println("waitForSwap 2");
            System.out.println(waitForSwap2.data);

        }
        currentNum2 = currentNum2.next;
    }

    currentNum1 = waitForSwap2;
    currentNum2 = waitForSwap1;

    System.out.println("currentNum1");
    System.out.println(currentNum1.data);

    System.out.println("currentNum2");
    System.out.println(currentNum2.data);
}

这是结果

如您所见,currentNum1currentNum2 相互更改,但打印结果并没有交换。如何交换两个节点及其数据?

编辑:下面的完整示例

节点类

public class Node {

public int data;
public Node next;

public Node(int _data) {
    this.data = _data;
    this.next = null;
}

public String toString() {
    return (Integer.toString(data));
}
}

链表类

public class LinkedList {

Node head;

public void Insert(int data) {
    Node node = new Node(data);

    if (head == null) {
        head = node;
    } else {
        Node n = head;
        while (n.next != null) {
            n = n.next;
        }
        n.next = node;
    }
}

public void ShowList() {
    Node node = head;
    while (node != null) {
        System.out.print(node.data + " ");
        node = node.next;
    }
}

public void swapNodes(int num1, int num2) {

    if (num1 == num2) {
        return;
    }

    Node currentNum1 = head;
    Node currentNum2 = head;
    Node waitForSwap1 = null;
    Node waitForSwap2 = null;
    while (currentNum1 != null) {
        if (currentNum1.data == num1) {
            waitForSwap1 = currentNum1;
            System.out.println();
            System.out.println("waitForSwap 1");
            System.out.println(waitForSwap1.data);

        }
        currentNum1 = currentNum1.next;
    }

    while (currentNum2 != null) {
        if (currentNum2.data == num2) {
            waitForSwap2 = currentNum2;
            System.out.println("waitForSwap 2");
            System.out.println(waitForSwap2.data);

        }
        currentNum2 = currentNum2.next;
    }

    currentNum1 = waitForSwap2;
    currentNum2 = waitForSwap1;

    System.out.println("currentNum1");
    System.out.println(currentNum1.data);

    System.out.println("currentNum2");
    System.out.println(currentNum2.data);
}
}

测试员

public class Runner {

public static void main(String[] args) {
    LinkedList lkdList = new LinkedList();

    lkdList.Insert(10);
    lkdList.Insert(9);
    lkdList.Insert(15);
    lkdList.Insert(2);
    lkdList.Insert(73);

    lkdList.ShowList();

    lkdList.swapNodes(10, 2);

    System.out.println();
    System.out.println("After Swap");

    lkdList.ShowList();
}

}

【问题讨论】:

  • 请提出minimal reproducible example,这样我们就可以简单地复制粘贴您的代码并进行调试。另外,为什么“交换节点而不交换数据”对你不起作用?
  • 您实际上并没有交换节点,您只是将找到的节点分配给 Node 对象并打印它们的值。为了交换,您还需要跟踪前一个节点,因为要交换,您也会更改 previous.next 的值。
  • 你没有改变你列表中的任何结构;您只需更改局部变量所指的内容。
  • @SergeiSirik 这是作业的要求,教授要我们同时更改节点和数据。
  • 所以,如果我做对了,这正是您正在寻找的“交换节点而不交换数据” - 这意味着节点对象将被交换,或者您有 2 个不同的分配:1。交换节点; 2 在不交换节点的情况下交换节点中的数据?

标签: java linked-list swap singly-linked-list


【解决方案1】:

好的,如果你只想交换数据,而不是节点,这里是:

  public void swapNodes(int num1, int num2) {

    if (num1 == num2) {
      return;
    }

    Node node1 = null;
    Node node2 = null;

    Node cur = head;
    while(cur != null) {
      if (num1 == cur.data) {
        node1 = cur;
      }
      if (num2 == cur.data) {
        node2 = cur;
      }
      cur = cur.next;
    }

    if (node1 == null || node2 == null)
      return;

    int tmp = node1.data;
    node1.data = node2.data;
    node2.data = tmp;
  }

【讨论】:

    猜你喜欢
    • 2017-05-05
    • 2010-12-04
    • 1970-01-01
    • 2015-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多