Java Program For Swapping Nodes In A Linked List Without Swapping Data
Last Updated :
30 Mar, 2022
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.
Examples:
Input : 10->15->12->13->20->14, x = 12, y = 20
Output: 10->15->20->13->12->14
Input : 10->15->12->13->20->14, x = 10, y = 20
Output: 20->15->12->13->10->14
Input : 10->15->12->13->20->14, x = 12, y = 13
Output: 10->15->13->12->20->14
This may look a simple problem, but is an interesting question as it has the following cases to be handled.
- x and y may or may not be adjacent.
- Either x or y may be a head node.
- Either x or y may be the last node.
- x and/or y may not be present in the linked list.
How to write a clean working code that handles all the above possibilities.
The idea is to first search x and y in the given linked list. If any of them is not present, then return. While searching for x and y, keep track of current and previous pointers. First change next of previous pointers, then change next of current pointers.
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;
}
}
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.print(
"Linked list before calling swapNodes() " );
llist.printList();
llist.swapNodes( 4 , 3 );
System.out.print(
"Linked list after calling swapNodes() " );
llist.printList();
}
}
|
Output:
Linked list before calling swapNodes() 1 2 3 4 5 6 7
Linked list after calling swapNodes() 1 2 4 3 5 6 7
Time Complexity: O(n)
Auxiliary Space: O(1)
Optimizations: The above code can be optimized to search x and y in single traversal. Two loops are used to keep program simple.
Simpler approach:
Java
public class Solution
{
class Node
{
int data;
Node next;
public Node( int data)
{
this .data = data;
this .next = null ;
}
}
public Node head = null ;
public Node tail = null ;
public void addNode( int data)
{
Node newNode = new Node(data);
if (head == null )
{
head = newNode;
tail = newNode;
}
else
{
tail.next = newNode;
tail = newNode;
}
}
public void swap( int n1, int n2)
{
Node prevNode1 = null , prevNode2 = null ,
node1 = head, node2 = head;
if (head == null )
{
return ;
}
if (n1 == n2)
return ;
while (node1 != null &&
node1.data != n1)
{
prevNode1 = node1;
node1 = node1.next;
}
while (node2 != null &&
node2.data != n2)
{
prevNode2 = node2;
node2 = node2.next;
}
if (node1 != null &&
node2 != null )
{
if (prevNode1 != null )
prevNode1.next = node2;
else
head = node2;
if (prevNode2 != null )
prevNode2.next = node1;
else
head = node1;
Node temp = node1.next;
node1.next = node2.next;
node2.next = temp;
}
else
{
System.out.println( "Swapping is not possible" );
}
}
public void display()
{
Node current = head;
if (head == null )
{
System.out.println( "List is empty" );
return ;
}
while (current != null )
{
System.out.print(current.data + " " );
current = current.next;
}
System.out.println();
}
public static void main(String[] args)
{
Solution sList = new Solution();
sList.addNode( 1 );
sList.addNode( 2 );
sList.addNode( 3 );
sList.addNode( 4 );
sList.addNode( 5 );
sList.addNode( 6 );
sList.addNode( 7 );
System.out.println( "Original list: " );
sList.display();
sList.swap( 6 , 1 );
System.out.println(
"List after swapping nodes: " );
sList.display();
}
}
|
Output:
Linked list before calling swapNodes() 1 2 3 4 5 6 7
Linked list after calling swapNodes() 6 2 3 4 5 1 7
Time Complexity: O(n)
Auxiliary Space: O(1)
Please refer complete article on Swap nodes in a linked list without swapping data for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...