Wednesday, July 3, 2024
HomeData ModellingData Structure & AlgorithmC# Program For Pairwise Swapping Elements Of A Given Linked List

C# 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 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:

C#




// C# program to pairwise swap elements
// of a linked list
using System;
class LinkedList
{
Ā Ā Ā Ā // Head of list
Ā Ā Ā Ā Node head;
Ā 
Ā Ā Ā Ā // Linked list Node
Ā Ā Ā Ā public class Node
Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā public int data;
Ā Ā Ā Ā Ā Ā Ā Ā public Node next;
Ā Ā Ā Ā Ā Ā Ā Ā public 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)
Ā Ā Ā Ā Ā Ā Ā Ā {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Console.Write(temp.data + " ");
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā temp = temp.next;
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine();
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā // 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);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "Linked List before calling pairWiseSwap() ");
Ā Ā Ā Ā Ā Ā Ā Ā llist.printList();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā llist.pairWiseSwap();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā Console.WriteLine(
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā "Linked List after calling pairWiseSwap() ");
Ā Ā Ā Ā Ā Ā Ā Ā llist.printList();
Ā Ā Ā Ā }
}
// This code is contributed by Arnab Kundu


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)

Auxiliary Space: O(1)

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:

C#




/* 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)
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!

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Last Updated :
04 Mar, 2023
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments