PUBLISHED ON: APRIL 20, 2021
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:
- Start
- Declare a linked list of integer types without any initial size.
- Use the add method to add the elements.
- Append the elements at the end of the list.
- Print the linked list elements before reversing.
- First link two nodes properly and then swap these nodes.
- As the previous link is broken, now again link the nodes.
- Print the linked list elements after reversing.
- 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
public class ReverseLinkedListInPair
{
private Node head;
private static class Node
{
private int value;
private Node next;
Node(int value) {
this.value = value;
}
}
public void addLast(Node node) {
if (head == null) {
head = node;
} else {
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = node;
}
}
public void printLinkedList(Node head) {
Node temp = head;
while (temp != null) {
System.out.format("%d ", temp.value);
temp = temp.next;
}
System.out.println();
}
// Reverse the linked list in pair
public static Node reverseLLInPairs(Node head)
{
Node current=head;
Node temp=null;
Node newHead =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;
if (newHead == null)
newHead = temp;
current=current.next;
}
return newHead;
}
//Driver Code
public static void main(String[] args) {
ReverseLinkedListInPair li = new ReverseLinkedListInPair();
// Creating a linked list
Node head=new Node(0);
li.addLast(head);
li.addLast(new Node(1));
li.addLast(new Node(2));
li.addLast(new Node(3));
li.addLast(new Node(4));
li.addLast(new Node(5));
System.out.println("Before reversing in pair: ");
li.printLinkedList(head);
//Reversing LinkedList in pairs
Node result=reverseLLInPairs(head);
System.out.println("After reversing in pair: ");
li.printLinkedList(result);
}
}
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:
- Start
- Declare a linked list of integer types without any initial size.
- Use the add method to add the elements.
- Append the elements at the end of the list.
- Print the linked list elements before reversing.
- First link two nodes properly and then call the recursive function to swap the elements.
- As the previous link is broken, now again link the nodes.
- Print the linked list elements after reversing.
- 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
public class ReverseLinkedListInPair
{
private Node head;
private static class Node
{
private int value;
private Node next;
Node(int value) {
this.value = value;
}
}
public void addLast(Node node) {
if (head == null) {
head = node;
} else {
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = node;
}
}
public void printLinkedList(Node head) {
Node temp = head;
while (temp != null) {
System.out.format("%d ", temp.value);
temp = temp.next;
}
System.out.println();
}
// Reverse the linked list in pair
public static Node reverseLLInPairs(Node head)
{
if (head == null || head.next == null) {
return head;
}
Node temp=head.next;
head.next=temp.next;
temp.next=head;
head.next=reverseLLInPairs(head.next);
return temp;
}
//Driver Code
public static void main(String[] args) {
ReverseLinkedListInPair li = new ReverseLinkedListInPair();
// Creating a linked list
Node head=new Node(0);
li.addLast(head);
li.addLast(new Node(1));
li.addLast(new Node(2));
li.addLast(new Node(3));
li.addLast(new Node(4));
li.addLast(new Node(5));
System.out.println("Before reversing in pair: ");
li.printLinkedList(head);
//Reversing LinkedList in pairs
Node result=reverseLLInPairs(head);
System.out.println("After reversing in pair: ");
li.printLinkedList(result);
}
}
Before reversing in pair:
0 1 2 3 4 5
After reversing in pair:
1 0 3 2 5 4