Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swap elements in LinkedList

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.

like image 999
Boolean Avatar asked May 15 '10 08:05

Boolean


People also ask

How do I swap elements in LinkedList?

Elements can be swapped using by swapping the elements inside the nodes, and by swapping the complete nodes. Swap the two elements in a Linked List using the Java. util. LinkedList.

Can you swap on a linked list?

Given a linked list and two keys in it, swap nodes for two given keys. Nodes should be swapped by changing links. Swapping data of nodes may be expensive in many situations when data contains many fields. It may be assumed that all keys in the linked list are distinct.

What will be the elements of given singly linked list 1 2 3 4 5 with first node as head after calling?

This is Expert Verified AnswerOne is data and the other is pointer. Data is the value and the pointer points next node in the singly linked list. The first node of the singly linked list is Head and the last node of the singly linked list is tail.


1 Answers

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

}

like image 156
user3181496 Avatar answered Oct 20 '22 21:10

user3181496