简体   繁体   English

如何在单链列表中交换两个节点及其内容?

[英]How do I swap two nodes with their content in a singly linked list?

I googled this, but all of them are talking "swap nodes without swapping data". 我用谷歌搜索,但是所有人都在谈论“交换节点而不交换数据”。

I tried to write a swap node method myself: 我试图自己写一个交换节点方法:

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);
}

Here's the result 这是结果 在此处输入图片说明

As you can see, currentNum1 and currentNum2 changed to each other, but the printed result is not swapped. 如您所见, currentNum1currentNum2更改,但是打印结果未交换。 How do I swap two nodes and their data? 如何交换两个节点及其数据?

Edit: the complete example below 编辑:下面的完整示例

Node Class 节点类别

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));
}
}

Linked List Class 链表类

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);
}
}

Tester 测试仪

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();
}

}

Ok, if you only want to swap data, not nodes, here it is: 好的,如果您只想交换数据,而不是节点,则为:

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

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

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