# Java Program to Reverse a Linked List in Pairs

In this tutorial, we will see how to reverse a linked list in pairs in java. LinkedList is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays. But before moving further, if you are not familiar with the concept of the linked list in java, then do check the article on Linked List in Java.

Input: Enter the Linked List elements: 6 7 8 4 5 3

Output: Before Reversing the Linked List: 6 7 8 9 5

After Reversing the Linked List: 7 6 9 8 3 5

This can be done by using the following methods:

Approach 1: By Iteration

Approach 2: By Recursion

Let us look at each of these approaches for a better understanding.

## Program 1: Java Program to Reverse a Linked List in Pairs

In this program, we will see how to reverse a linked list in pairs in java by using the iterative approach.

### Algorithm:

1. Start
2. Declare a linked list of integer types without any initial size.
4. Append the elements at the end of the list.
5. Print the linked list elements before reversing.
6. First link two nodes properly and then swap these nodes.
7. As the previous link is broken, now again link the nodes.
8. Print the linked list elements after reversing.
9. Stop

Let us look at the below example for a better understanding of the above algorithm.

``````//Java Program to Reverse a linked list in Pairs
{
private static class Node
{
private int value;
private Node next;
Node(int value) {
this.value = value;
}
}
} else {
while (temp.next != null)
temp = temp.next;
temp.next = node;
}
}
while (temp != null) {
System.out.format("%d ", temp.value);
temp = temp.next;
}
System.out.println();
}
// Reverse the linked list in pair
{
Node temp=null;
while (current != null && current.next != null)
{
if (temp != null)
{
temp.next.next = current.next;
}
temp=current.next;
current.next=temp.next;
temp.next=current;
current=current.next;
}
}
//Driver Code
public static void main(String[] args) {
System.out.println("Before reversing in pair: ");
System.out.println("After reversing in pair: ");
}
}``````

Before reversing in pair:
0 1 2 3 4 5
After reversing in pair:
1 0 3 2 5 4

## Program 2: Java Program to Reverse the LinkedList in Pairs

In this program, we will see how to reverse a linked list in pairs in java by using the recursive approach.

### Algorithm:

1. Start
2. Declare a linked list of integer types without any initial size.
4. Append the elements at the end of the list.
5. Print the linked list elements before reversing.
6. First link two nodes properly and then call the recursive function to swap the elements.
7. As the previous link is broken, now again link the nodes.
8. Print the linked list elements after reversing.
9. Stop

Let us look at the below example for a better understanding of the above algorithm.

``````//Java Program to Reverse a linked list in Pairs
{
private static class Node
{
private int value;
private Node next;
Node(int value) {
this.value = value;
}
}
} else {
while (temp.next != null)
temp = temp.next;
temp.next = node;
}
}
while (temp != null) {
System.out.format("%d ", temp.value);
temp = temp.next;
}
System.out.println();
}
// Reverse the linked list in pair
{
}
return temp;
}
//Driver Code
public static void main(String[] args) {
System.out.println("Before reversing in pair: ");