Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6376 |

@mb1973 | 5578 |

@Quandray | 5231 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4510 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3412 |

Complete Leaderboard | |

Handle | Score |

@codersgroup18 | 714 |

@Manikanta punnam | 579 |

@sriramgoparaju99 | 532 |

@yashkaril4 | 517 |

@praveenbgp6 | 517 |

@prankursharma31 | 489 |

@rdakka | 465 |

@sonamnigam1999 | 441 |

@purohitmn02 | 427 |

@sonamkumari63928 | 426 |

@ShubhankarPandey | 419 |

Complete Leaderboard |

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 1:**

**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.

**Example 1:**

**Input:
**LinkedList: 1->3->4->7->9->10->1
**Output: 3** 1 7 4 10 9 1**
Explanation: **After swapping each pair
considering (1,3), (4, 7), (9, 10).. so
on as pairs, we get 3, 1, 7, 4, 10, 9,
1 as a new linked list.

**Your Task:**

The task is to complete the function **pairWiseSwap**() which takes the head node as the only argument and returns the head of modified linked list.

**Expected Time Complexity: **O(N).

**Expected Auxiliary Space: **O(1).

**Constraints:**

1 ≤ N ≤ 10^{3}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Pairwise swap elements of a linked list

...