Java Program For Pairwise Swapping Elements Of A Given Linked List
Last Updated :
23 Apr, 2022
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 example, if the linked list is 1->2->3->4->5 then the function should change it to 2->1->4->3->5, and if the linked list is then the function should change it to.
METHOD 1 (Iterative):
Start from the head node and traverse the list. While traversing swap data of each node with its next node's data.
Below is the implementation of the above approach:
Java
// Java program to pairwise swap
// elements of a linked list
class LinkedList
{
// Head of list
Node head;
// Linked list Node
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
void pairWiseSwap()
{
Node temp = head;
/* Traverse only till there are
atleast 2 nodes left */
while (temp != null &&
temp.next != null)
{
// Swap the data
int k = temp.data;
temp.data = temp.next.data;
temp.next.data = k;
temp = temp.next.next;
}
}
// Utility functions
/* Inserts a new Node at front of
the list. */
public void push(int new_data)
{
/* 1 & 2: Allocate the Node &
Put in the data*/
Node new_node = new Node(new_data);
// 3. Make next of new Node as head
new_node.next = head;
// 4. Move the head to point to
// new Node
head = new_node;
}
// Function to print linked list
void printList()
{
Node temp = head;
while (temp != null)
{
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
// Driver code
public static void main(String args[])
{
LinkedList llist = new LinkedList();
/* Created Linked List
1->2->3->4->5 */
llist.push(5);
llist.push(4);
llist.push(3);
llist.push(2);
llist.push(1);
System.out.println(
"Linked List before calling pairWiseSwap() ");
llist.printList();
llist.pairWiseSwap();
System.out.println(
"Linked List after calling pairWiseSwap() ");
llist.printList();
}
}
// This code is contributed by Rajat Mishra
Output:
Linked list before calling pairWiseSwap()
1 2 3 4 5
Linked list after calling pairWiseSwap()
2 1 4 3 5
Time complexity: O(n), as we are traversing over the linked list of size N using a while loop.
Auxiliary Space: O(1), as we are not using any extra space.
METHOD 2 (Recursive):
If there are 2 or more than 2 nodes in Linked List then swap the first two nodes and recursively call for rest of the list.
Below image is a dry run of the above approach:

Below is the implementation of the above approach:
Java
/* Recursive function to pairwise swap
elements of a linked list */
static void pairWiseSwap(node head)
{
/* There must be at-least two nodes
in the list */
if (head != null &&
head.next != null)
{
/* Swap the node's data with data
of next node */
swap(head.data, head.next.data);
/* Call pairWiseSwap() for rest of
the list */
pairWiseSwap(head.next.next);
}
}
// This code is contributed by aashish1995
Time complexity: O(n), as we are traversing over the linked list of size N using recursion.
Auxiliary Space: O(1), as we are not using any extra space.
The solution provided there swaps data of nodes. If data contains many fields, there will be many swap operations. See this for an implementation that changes links rather than swapping data.
Please refer complete article on Pairwise swap elements of a given linked list for more details!
Similar Reads
Java Program For Pairwise Swapping Elements Of A Given Linked List By Changing Links 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 sh
4 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 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 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 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 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 Swapping Nodes In A Linked List Without Swapping Data 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-
6 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
Java Program For Rotating A Linked List Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer. For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list should be modified to 50->60->10->20->30->40. Assume that k is smal
6 min read
Java Program For Deleting A Node In A Doubly Linked List Pre-requisite: Doubly Link List Set 1| Introduction and Insertion Write a function to delete a given node in a doubly-linked list. Original Doubly Linked List Recommended: Please solve it on "PRACTICE" first, before moving on to the solution. Approach: The deletion of a node in a doubly-linked list
4 min read