• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
October 08, 2023 |1.6K Views
PROBLEM OF THE DAY : 07/10/2023 | Pairwise Swap Elements of a Linked List
Description
Discussion

Welcome to the daily solving of our PROBLEM OF THE DAY with Siddharth Hazra. We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Data Structures and Algorithms but will also help you build up problem-solving skills.
 

Given a singly linked list of size N. The task is to swap elements in the linked list pairwise.
For example, if the input list is 1 2 3 4, the resulting list after swaps will be 2 1 4 3.

 

Note: You need to swap the nodes, not only the data. If only data is swapped then driver will print -1.

 

Example:

Input:

LinkedList: 1->2->2->4->5->6->7->8

Output: 

2 1 4 2 6 5 8 7

 

Explanation: 

After swapping each pair considering (1,2), (2, 4), (5, 6).. so on as pairs, we get 2, 1, 4, 2, 6, 5, 8, 7 as a new linked list.

 

Problem Link: https://practice.geeksforgeeks.org/problems-preview/pairwise-swap-elements-of-a-linked-list-by-swapping-data/1

Solution Function Link: https://ide.geeksforgeeks.org/online-cpp-compiler/eb936971-b40e-4e82-8c07-62a55c9c92f8