Java program to reverse linked list in pairs

If you want to practice data structure and algorithm programs, you can go through data structure and algorithm interview questions.

This is one of popular interview question. In this post, we will see how to reverse linked list in pairs.

Java Linked List Interview Programs:

There can be two solution for reversing linked list in pairs

• Iterative
• Recursive

Iterative:

Logic for this would be:

Explanation:

Lets understand with the help of simple example:
Lets say linkedlist is 5-> 6 -> 7 -> 1
If you look closely, below steps are just reversing link to  6->5->7->1 in first iteration (Swapping node 6 and 5) and then advance to next pair (Node 7 and 1)

You must be wondering why we have put below code:

before swapping two nodes , we need to link nodes properly. When we are swapping 7 and 1 , link between 5->7 will be broken and we need to connect node 5 to node 1.
As per above code , temp will be null in first iteration(swapping node 6 and 5) but when current node is 7, temp will be 6, here we are linking nodes as below
6->5->1->7

Java program for this will be :

Run above program, you will get following output:

Recursive:

Base case: Base case for this would be either node is null or node.next is null
For recursive solution, replace reverseLinkedListInPairs of above program to below function

Output of this program will be same as above program.

Please go through java interview programs for more such programs.

import_contacts

Related Posts

• 12 August

Intersection of two linked lists

Table of ContentsProblemSolutionWas this post helpful? If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post, we will see how to find Intersection of two linked lists. Problem Given two singly linked lists, find if two linked lists intersect. If they intersect, find intersection point. […]

• 12 October

Implement Queue using Linked List in java

If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post , we will see how to implement Queue using Linked List in java. Queue is abstract data type which demonstrates First in first out (FIFO) behaviour. We will implement same behaviour using Array. Although java provides implementation […]

• 10 October

Convert sorted Linked List to balanced BST

Table of ContentsJava Program to convert sorted LinkedList to balanced BST:Was this post helpful? If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post , we will see how to convert sorted LinkedList to balanced binary search tree. There are two ways to do […]

• 20 September

Find length of Linked List in java

Table of ContentsJava program to find length of LinkedList:Was this post helpful? If you want to practice data structure and algorithm programs, you can go through data structure and algorithm programs. In this post, we will see how to find length of Linked List in java. You can obviously use size() method of java Linked List […]

• 20 September

Implement singly linked list in java

In this post, we will see how to implement singly linked list in java. It is one of the most used data structure. In singly linked list, Node has data and pointer to next node. It does not have pointer to the previous node. Last node ‘s next points to null, so you can iterate […]

• 10 September

Implement stack using Linked List in java

If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this program, we will see how to implement stack using Linked List in java. The Stack is an abstract data type that demonstrates Last in first out (LIFO) behavior. We will implement the same behavior using […]

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.