Java Program For Pairwise Swapping Elements Of A Given Linked List By Changing Links
Last Updated :
30 Mar, 2022
Given a singly linked list, write a function to swap elements pairwise. For example, if the linked list is 1->2->3->4->5->6->7 then the function should change it to 2->1->4->3->6->5->7, and if the linked list is 1->2->3->4->5->6 then the function should change it to 2->1->4->3->6->5
This problem has been discussed here. The solution provided there swaps data of nodes. If data contains many fields, there will be many swap operations. So changing links is a better idea in general. Following is the implementation that changes links instead of swapping data.
Java
class LinkedList
{
static Node head;
static class Node
{
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
Node pairWiseSwap(Node node)
{
if (node == null ||
node.next == null )
{
return node;
}
Node prev = node;
Node curr = node.next;
node = curr;
while ( true )
{
Node next = curr.next;
curr.next = prev;
if (next == null ||
next.next == null )
{
prev.next = next;
break ;
}
prev.next = next.next;
prev = next;
curr = prev.next;
}
return node;
}
void printList(Node node)
{
while (node != null )
{
System.out.print(node.data +
" " );
node = node.next;
}
}
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node( 1 );
list.head.next = new Node( 2 );
list.head.next.next = new Node( 3 );
list.head.next.next.next =
new Node( 4 );
list.head.next.next.next.next =
new Node( 5 );
list.head.next.next.next.next.next =
new Node( 6 );
list.head.next.next.next.next.next.next =
new Node( 7 );
System.out.println(
"Linked list before calling pairwiseSwap() " );
list.printList(head);
Node st = list.pairWiseSwap(head);
System.out.println( "" );
System.out.println(
"Linked list after calling pairwiseSwap() " );
list.printList(st);
System.out.println( "" );
}
}
|
Output:
Linked list before calling pairWiseSwap() 1 2 3 4 5 6 7
Linked list after calling pairWiseSwap() 2 1 4 3 6 5 7
Time Complexity: The time complexity of the above program is O(n) where n is the number of nodes in a given linked list. The while loop does a traversal of the given linked list.
Auxiliary Space: O(1)
Following is the recursive implementation of the same approach. We change the first two nodes and recur for the remaining list. Thanks to geek and omer salem for suggesting this method.
Java
class LinkedList
{
static Node head;
static class Node
{
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
Node pairWiseSwap(Node node)
{
if (node == null ||
node.next == null )
{
return node;
}
Node remaining = node.next.next;
Node newhead = node.next;
node.next.next = node;
node.next = pairWiseSwap(remaining);
return newhead;
}
void printList(Node node)
{
while (node != null )
{
System.out.print(node.data +
" " );
node = node.next;
}
}
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node( 1 );
list.head.next = new Node( 2 );
list.head.next.next = new Node( 3 );
list.head.next.next.next =
new Node( 4 );
list.head.next.next.next.next =
new Node( 5 );
list.head.next.next.next.next.next =
new Node( 6 );
list.head.next.next.next.next.next.next =
new Node( 7 );
System.out.println(
"Linked list before calling pairwiseSwap() " );
list.printList(head);
head = list.pairWiseSwap(head);
System.out.println( "" );
System.out.println(
"Linked list after calling pairwiseSwap() " );
list.printList(head);
System.out.println( "" );
}
}
|
Output:
Linked list before calling pairWiseSwap() 1 2 3 4 5 6 7
Linked list after calling pairWiseSwap() 2 1 4 3 6 5 7
Time Complexity: O(n)
Auxiliary Space: O(n)
Please refer complete article on Pairwise swap elements of a given linked list by changing links for more details!
Share your thoughts in the comments
Please Login to comment...