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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.kkjavatutorials.util; /** * This class represent Node in Linked List * @author KK JavaTutorials */ public class Node<T>{ //Data hold by LinkedList Node public T data; //Reference to point next Node public Node<T> next; public Node(T data) { super(); this.data = data; this.next = null; } } |
LinkedList.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
package com.kkjavatutorials.util; /** * Singly LinkedList Implementation * * @author KK JavaTutorials */ public class LinkedList<T> { // head always point to first node private Node<T> head; /** * Method to insert Node in Linked List * * @param newNode insert to List */ public void insert(Node<T> newNode) { if (isEmpty()) { head = newNode; } else { Node<T> current = head; while (current.next != null) { current = current.next; } current.next = newNode; } } // Check if Linked is empty private boolean isEmpty() { return head == null; } /** * Method which traverse Linked List and display all data. */ public void displayLinkedList() { Node<T> currentNode = head; while (currentNode != null) { System.out.print(currentNode.data + " "); currentNode = currentNode.next; } } /** * This Method Reverse Singly LinkedList in Pairs * @param head of LinkedList * @return */ public void reverseLinkedListInPairs() { Node<T> tempNode = head; /* Traverse the List only till there are at least 2 nodes left */ while (tempNode != null && tempNode.next != null) { /* Swap the data */ T k = tempNode.data; tempNode.data = tempNode.next.data; tempNode.next.data = k; tempNode = tempNode.next.next; } } } |
ClientTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.kkjavatutorials.util; /** * @author KK JavaTutorials * Client program to Reverse linked list in pairs ? */ public class ClientTest { public static void main(String[] args) { //Create LinkedList Instance LinkedList<Integer> linkedList = new LinkedList<>(); linkedList.insert(new Node<Integer>(1)); linkedList.insert(new Node<Integer>(2)); linkedList.insert(new Node<Integer>(3)); linkedList.insert(new Node<Integer>(4)); System.out.println("Original LinkedList:::"); linkedList.displayLinkedList(); System.out.println(); linkedList.reverseLinkedListInPairs(); System.out.println("LinkedList after Reverse in Pairs:::"); linkedList.displayLinkedList(); } } |
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.