Java program to swap two elements in a LinkedList

Given a linked list, the task is to exchange two elements without disturbing their links. There are multiple exchange methods. The elements can be exchanged by exchanging the elements within the node and exchanging the complete node.

Example:

Input :- 10->11->12->13->14->15
         element1 = 11
         element2 = 14
         
Output :- 10->14->12->13->11->15

Method 1: Using the in-built set method

Use the Java.util.LinkedList.set() method to exchange two elements in the linked list. In order to achieve the output we want, first, make sure that the two elements provided to us are both available in the linked list. If any element does not exist, just return. Use the set() method to set the position of element1 to the position of element2 and vice versa. Here is the code for the above method:

 

// Swaping two elements in a Linked List in Java

import java.util.*;

class GFG {
	public static void main(String[] args)
	{

		LinkedList ll = new LinkedList<>();

		// Adding elements to Linked List
		ll.add(10);
		ll.add(11);
		ll.add(12);
		ll.add(13);
		ll.add(14);
		ll.add(15);

		// Elements to swap
		int element1 = 11;
		int element2 = 14;

		System.out.println("Linked List Before Swaping :-");

		for (int i : ll) {
			System.out.print(i + " ");
		}

		// Swapping the elements
		swap(ll, element1, element2);
		System.out.println();
		System.out.println();

		System.out.println("Linked List After Swaping :-");

		for (int i : ll) {
			System.out.print(i + " ");
		}
	}

	// Swap Function
	public static void swap(LinkedList list,
							int ele1, int ele2)
	{

		// Getting the positions of the elements
		int index1 = list.indexOf(ele1);
		int index2 = list.indexOf(ele2);

		// Returning if the element is not present in the
		// LinkedList
		if (index1 == -1 || index2 == -1) {
			return;
		}

		// Swapping the elements
		list.set(index1, ele2);
		list.set(index2, ele1);
	}
}

Output

Linked List Before Swaping :-
10 11 12 13 14 15 

Linked List After Swaping :-
10 14 12 13 11 15

Time Complexity: O(N), where N is the length of Linked List

Method 2: Using our very own Linked List

 

A linked list is given which has two nodes which are rewarded and swapped for the given two nodes.

Below is the implementation of the above approach:

// Java Program to Swap Two Elements in a LinkedList
class Node {
	int data;
	Node next;
	Node(int d)
	{
		data = d;
		next = null;
	}
}

class LinkedList {
	Node head; // head of list

	// Function to swap Nodes x and y in
	// linked list by changing links
	public void swapNodes(int x, int y)
	{
		// Nothing to do if x and y are same
		if (x == y)
			return;

		// Search for x (keep track of prevX and CurrX)
		Node prevX = null, currX = head;
		while (currX != null && currX.data != x) {
			prevX = currX;
			currX = currX.next;
		}

		// Search for y (keep track of prevY and currY)
		Node prevY = null, currY = head;
		while (currY != null && currY.data != y) {
			prevY = currY;
			currY = currY.next;
		}

		// If either x or y is not present, nothing to do
		if (currX == null || currY == null)
			return;

		// If x is not head of linked list
		if (prevX != null)
			prevX.next = currY;
		else // make y the new head
			head = currY;

		// If y is not head of linked list
		if (prevY != null)
			prevY.next = currX;
		else // make x the new head
			head = currX;

		// Swap next pointers
		Node temp = currX.next;
		currX.next = currY.next;
		currY.next = temp;
	}

	// Function to add Node at beginning of list.
	public void push(int new_data)
	{
		// 1. alloc the Node and put the data
		Node new_Node = new Node(new_data);

		// 2. Make next of new Node as head
		new_Node.next = head;

		// 3. Move the head to point to new Node
		head = new_Node;
	}

	// This function prints contents of linked
	// list starting from the given Node
	public void printList()
	{
		Node tNode = head;
		while (tNode != null) {
			System.out.print(tNode.data + " ");
			tNode = tNode.next;
		}
		System.out.println();
	}

	// Driver program to test above function
	public static void main(String[] args)
	{
		LinkedList llist = new LinkedList();

		// The constructed linked list is:
		// 1->2->3->4->5->6->7
		llist.push(7);
		llist.push(6);
		llist.push(5);
		llist.push(4);
		llist.push(3);
		llist.push(2);
		llist.push(1);

		System.out.println("Linked List Before Swaping :-");
		llist.printList();

		llist.swapNodes(4, 3);

		System.out.println("Linked List After Swaping :-");
		llist.printList();
	}
}

Output

Linked List Before Swaping :-
1 2 3 4 5 6 7 
Linked List After Swaping :-
1 2 4 3 5 6 7

Time Complexity: O(N), where N is the length of Linked List

Submit Your Programming Assignment Details