Reverse linked list in pairs ?

By | July 12, 2020

In this post, we will learn how to write a java program to reverse the linked list in pairs.

Let’s try to understand this problem statement with an Example.

If We have a LinkedList that holds 1->2->3->4  then after the function has been called the LinkedList would hold 2->1->4->3

Logic is very simple:

We have to start from the head node and traverse the list and while traversing swap data of each node with its next node’s data.

Complete Source is given Below:

Node.java

 

LinkedList.java

 

ClientTest.java

 

Output Of this Program:

Original LinkedList:::
1 2 3 4
LinkedList after Reverse in Pairs:::
2 1 4 3

Time Complexity: O(n) and Space Complexity: O(1)

You May Also Like:

How to remove a given key from the Singly Linked List in Java ?
How to find the middle node in a Singly Linked List in Java ?
How to search an element in a Singly Linked List in Java ?
How to find the length of a Singly Linked List in Java?
Implementation to reverse a Singly Linked List in Java ?
How to check if LinkedList is palindrome or not in java ?
How to convert sorted Linked List to balanced Binary Search Tree?
How to find the intersection of two LinkedList in java
How to find Nth node from the end of a Singly Linked List in Java?

That’s all about the Reverse linked list in pairs?
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published.