Swap elements in LinkedList
Asked Answered
M

7

10

I want to maintain order of the elements being added in a list. So, I used a LinkedList in Java.

Now I want to be able to swap two elements in the linked list. First of all, I cannot find an elementAt() for LinkedList. Also, there is no way to add element at a specified position.

Misspell answered 15/5, 2010 at 8:21 Comment(0)
I
24

There is a Collections.swap(List<?> list, int i, int j) that you can use to swap two elements of a List<?>. There's also LinkedList.get(int index) and LinkedList.add(int index, E element) (both are methods specified by interface List). All of these operations will be O(N) since a LinkedList does not implements RandomAccess.

Incubator answered 15/5, 2010 at 8:25 Comment(0)
B
2

Check out the Javadocs for LinkedList

To find an element at an index use get(int index)

To place an element at a certain index use set(int index, Object element)

Baryram answered 15/5, 2010 at 8:29 Comment(0)
P
2

If you are writing your own LinkedList class for exercise (i.e. for a project or school), try making two temporary Object variables and two ints to hold their position in the List. Then, use add(int, Object) to add the first in the 2nd position, second in the 1st position.

Popper answered 5/12, 2012 at 13:8 Comment(0)
T
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;
    }
}

}

Taxiway answered 2/12, 2016 at 10:42 Comment(0)
B
0

add

Does this what you want?

If you want to keep the list in a sorted state, why not just insert the element with addfirst

and then sort the list using Collections.sort

Blacking answered 15/5, 2010 at 8:28 Comment(0)
S
0

Take a look at ArrayList , this class will both maintain the insertion order and provide O(1) random access.

Symptomatology answered 15/5, 2010 at 8:38 Comment(0)
K
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;
    }
Kvass answered 6/11, 2020 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.