在LinkedList中交换元素

10

我希望能够维护列表中添加元素的顺序。因此,我在Java中使用了LinkedList

现在我想要交换链表中的两个元素。首先,我无法找到LinkedListelementAt()方法。而且,也没有一种方法可以在指定位置添加元素。

7个回答

25

2

请查看LinkedList的Javadocs。

要在index处找到一个元素,请使用get(int index)

要将一个element放置在特定的index处,请使用set(int index, Object element)


2

如果你在练习中(比如项目或学校)编写自己的LinkedList类,可以尝试创建两个临时Object变量和两个int变量来保存它们在列表中的位置。然后,使用add(int, Object)方法将第一个变量添加到第二个位置,第二个变量添加到第一个位置。


1
public class SwapNode {

public static Node head;

public static void main(String[] args) {
    SwapNode obj = new SwapNode();
    obj.insertAtEnd(5);
    obj.insertAtEnd(6);
    obj.insertAtEnd(4);
    obj.insertAtEnd(7);
    obj.insertAtEnd(3);
    obj.insertAtEnd(8);
    obj.insertAtEnd(2);
    obj.insertAtEnd(9);
    obj.insertAtEnd(1);
    obj.print(head);
    System.out.println("*** Swapped ***");
    obj.swapElementValue(4, 2);     
}

public void swapElementValue(int value1, int value2) {
    if (value1 == value2) {
        System.out.println("Values same, so no need to swap");
        return;
    }
    boolean found1 = false, found2 = false; 
    Node node = head;
    while (node != null && !(found1 && found2)) {
        if (node.data == value1) {
            node.data = value2;
            found1 = true;
            node = node.next;
            continue;
        }
        if (node.data == value2) {
            node.data = value1;
            found2 = true;
            node = node.next;
            continue;
        }
        node = node.next;
    }
    if (found1 && found2) {
        print(head);
    } else {
        System.out.println("Values not found");
    }
}

public void insertAtEnd(int data) {
    Node newNode = new Node(data);
    if (head == null) {
        head = newNode;
        return;
    }

    Node temp = head;
    while (temp.next != null) {
        temp = temp.next;
    }
    temp.next = newNode;
}

public void print(Node head) {
    Node temp = head;
    while(temp != null) {
        System.out.print(temp.data);
        temp = temp.next;
    }
    System.out.println();
}


static class Node {
    private int data;
    public Node next;

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

}


0

add

这是你想要的吗?

如果你想保持列表的排序状态,为什么不使用addfirst插入元素呢?

然后使用Collections.sort对列表进行排序。


0

看一下ArrayList,这个类将同时维护插入顺序并提供O(1)的随机访问。


0
 // I tried to reduce time complexity here, in 3 while loops (get() and set() use 4 while loop)
   void swapAt(int index1, int index2){ // swapping at index
        Node tmp = head;
        int count=0;
        int min, max;   // for future reference to reduce time complexity
        if(index1<index2){
             min = index1;
             max = index2;
        }
        else{
             min = index2;
             max = index1;
        }    
        int diff = max - min;
        while(min!=count){
            tmp=  tmp.next;
            count++;
        }
        int minValue = tmp.data; 
        while(max!=count){
            tmp=  tmp.next;
            count++;
        }
        int maxValue = tmp.data;
        tmp.data = minValue;
        tmp = head;
        count =0;
        while(min!=count){
            tmp=  tmp.next;
            count++;
        }
        tmp.data = maxValue;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接