Given a Linked List, the task is to swap two elements without disturbing their links. There are multiple ways to swap. Elements can be swapped using by swapping the elements inside the nodes, and by swapping the complete nodes.
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
Swap the two elements in a Linked List using the Java.util.LinkedList.set() method. In order to achieve our desired output, first, make sure that both the elements provided to us are available in the Linked List. If either of the elements is absent, simply return. Use the set() method to set the element1’s position to element2’s and vice versa.
Below is the code for the above approach:
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
LinkedList<Integer> ll = new LinkedList<>();
ll.add( 10 );
ll.add( 11 );
ll.add( 12 );
ll.add( 13 );
ll.add( 14 );
ll.add( 15 );
int element1 = 11 ;
int element2 = 14 ;
System.out.println( "Linked List Before Swapping :-" );
for ( int i : ll) {
System.out.print(i + " " );
}
swap(ll, element1, element2);
System.out.println();
System.out.println();
System.out.println( "Linked List After Swapping :-" );
for ( int i : ll) {
System.out.print(i + " " );
}
}
public static void swap(LinkedList<Integer> list,
int ele1, int ele2)
{
int index1 = list.indexOf(ele1);
int index2 = list.indexOf(ele2);
if (index1 == - 1 || index2 == - 1 ) {
return ;
}
list.set(index1, ele2);
list.set(index2, ele1);
}
}
|
Output
Linked List Before Swapping :-
10 11 12 13 14 15
Linked List After Swapping :-
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
Given a linked list, provided with two values, and swap nodes for two given nodes.
Below is the implementation of the above approach:
Java
class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
class LinkedList {
Node head;
public void swapNodes( int x, int y)
{
if (x == y)
return ;
Node prevX = null , currX = head;
while (currX != null && currX.data != x) {
prevX = currX;
currX = currX.next;
}
Node prevY = null , currY = head;
while (currY != null && currY.data != y) {
prevY = currY;
currY = currY.next;
}
if (currX == null || currY == null )
return ;
if (prevX != null )
prevX.next = currY;
else
head = currY;
if (prevY != null )
prevY.next = currX;
else
head = currX;
Node temp = currX.next;
currX.next = currY.next;
currY.next = temp;
}
public void push( int new_data)
{
Node new_Node = new Node(new_data);
new_Node.next = head;
head = new_Node;
}
public void printList()
{
Node tNode = head;
while (tNode != null ) {
System.out.print(tNode.data + " " );
tNode = tNode.next;
}
System.out.println();
}
public static void main(String[] args)
{
LinkedList llist = new LinkedList();
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 Swapping :-" );
llist.printList();
llist.swapNodes( 4 , 3 );
System.out.println( "Linked List After Swapping :-" );
llist.printList();
}
}
|
Output
Linked List Before Swapping :-
1 2 3 4 5 6 7
Linked List After Swapping :-
1 2 4 3 5 6 7
Time Complexity: O(N), where N is the length of Linked List
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!