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
// Java program to swap elements of
// linked list by changing links
class LinkedList
{
static Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
/* Function to pairwise swap elements
of a linked list */
Node pairWiseSwap(Node node)
{
// If linked list is empty or
// there is only one node in list
if (node == null ||
node.next == null)
{
return node;
}
// Initialize previous and
// current pointers
Node prev = node;
Node curr = node.next;
// Change head before proceeding
node = curr;
// Traverse the list
while (true)
{
Node next = curr.next;
// Change next of current as
// previous node
curr.next = prev;
// If next NULL or next is the
// last node
if (next == null ||
next.next == null)
{
prev.next = next;
break;
}
// Change next of previous to
// next next
prev.next = next.next;
// Update previous and curr
prev = next;
curr = prev.next;
}
return node;
}
/* Function to print nodes in a
given linked list */
void printList(Node node)
{
while (node != null)
{
System.out.print(node.data +
" ");
node = node.next;
}
}
// Driver code
public static void main(String[] args)
{
/* The constructed linked list is:
1->2->3->4->5->6->7 */
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("");
}
}
// This code is contributed by Mayank Jaiswal
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
// Java program to swap elements of
// linked list by changing links
class LinkedList
{
static Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
/* Function to pairwise swap elements
of a linked list. It returns head
of the modified list, so return
value of this node must be assigned */
Node pairWiseSwap(Node node)
{
// Base Case: The list is empty or
// has only one node
if (node == null ||
node.next == null)
{
return node;
}
// Store head of list after two
// nodes
Node remaining = node.next.next;
// Change head
Node newhead = node.next;
// Change next of second node
node.next.next = node;
// Recur for remaining list and change
// next of head
node.next = pairWiseSwap(remaining);
// Return new head of modified list
return newhead;
}
/* Function to print nodes in a
given linked list */
void printList(Node node)
{
while (node != null)
{
System.out.print(node.data +
" ");
node = node.next;
}
}
// Driver code
public static void main(String[] args)
{
/* The constructed linked list is:
1->2->3->4->5->6->7 */
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!
Similar Reads
Java Program For Pairwise Swapping Elements Of A Given Linked List Given a singly linked list, write a function to swap elements pairwise. Input: 1->2->3->4->5->6->NULL Output: 2->1->4->3->6->5->NULL Input: 1->2->3->4->5->NULL Output: 2->1->4->3->5->NULL Input: 1->NULL Output: 1->NULL For examp
3 min read
Java Program For Sorting A Linked List Of 0s, 1s And 2s By Changing Links Given a linked list of 0s, 1s and 2s, sort it.Examples: Input: 2->1->2->1->1->2->0->1->0 Output: 0->0->1->1->1->1->2->2->2 The sorted Array is 0, 0, 1, 1, 1, 1, 2, 2, 2. Input: 2->1->0 Output: 0->1->2 The sorted Array is 0, 1, 2Recommended: Please solve it on "PRACTICE" first, before moving on to th
3 min read
Java Program For Deleting A Linked List Node At A Given Position Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7 Output: Linked List = 8->3->1->7 Input: position = 0, Linked List = 8->2->3->1->7 Output: Linked List = 2->3->1
3 min read
Java Program For Arranging Single Linked List In Alternate Odd and Even Nodes Order Given a singly linked list, rearrange the list so that even and odd nodes are alternate in the list.There are two possible forms of this rearrangement. If the first data is odd, then the second node must be even. The third node must be odd and so on. Notice that another arrangement is possible where
8 min read
Java Program For Segregating Even And Odd Nodes In A Linked List Given a Linked List of integers, write a function to modify the linked list such that all even numbers appear before all the odd numbers in the modified linked list. Also, keep the order of even and odd numbers same.Examples: Input: 17->15->8->12->10->5->4->1->7->6->NUL
6 min read
Java Program For Alternating Split Of A Given Singly Linked List- Set 1 Write a function AlternatingSplit() that takes one list and divides up its nodes to make two smaller lists 'a' and 'b'. The sublists should be made from alternating elements in the original list. So if the original list is 0->1->0->1->0->1 then one sublist should be 0->0->0 and
2 min read
Java Program For Rearranging A Given Linked List In-Place. Given a singly linked list L0 -> L1 -> ⦠-> Ln-1 -> Ln. Rearrange the nodes in the list so that the new formed list is : L0 -> Ln -> L1 -> Ln-1 -> L2 -> Ln-2 ...You are required to do this in place without altering the nodes' values. Examples: Input: 1 -> 2 -> 3 -
8 min read
Java Program For Swapping Kth Node From Beginning With Kth Node From End In A Linked List Given a singly linked list, swap kth node from beginning with kth node from end. Swapping of data is not allowed, only pointers should be changed. This requirement may be logical in many situations where the linked list data part is huge (For example student details line Name, RollNo, Address, ..etc
5 min read
Java Program For Moving Last Element To Front Of A Given Linked List Write a function that moves the last element to the front in a given Singly Linked List. For example, if the given Linked List is 1->2->3->4->5, then the function should change the list to 5->1->2->3->4. Algorithm: Traverse the list till the last node. Use two pointers: one t
3 min read
Java Program to Search an Element in a Linked List Prerequisite: LinkedList in java LinkedList is a linear data structure where the elements are not stored in contiguous memory locations. Every element is a separate object known as a node with a data part and an address part. The elements are linked using pointers or references. Linked Lists are pre
5 min read