AfterAcademy Tech
•
20 Jan 2020

Difficulty: Medium
Asked in: Amazon, Microsoft
Given a Linked List, your task is to swap the nodes of the Linked List in pairs. Swapping the Linked List in pairs is equivalent to reversing a Linked List in a group of two.
Problem Note: For Linked List with odd number of elements, the one node at the last would not be swapped while for the one with even number of nodes, all the nodes will be swapped in pair.
For example:
Input: 1->2->3->4->5->NULL
Output: 2->1->4->3->5->NULL
Similarly
Input: 5->6->7->8->NULL
Output: 6->5->8->7->NULL
Explanation: The nodes are swapped pairwise that is with its adjacent nodes.
Possible follow-up questions to ask the interviewer: →
There is very vivid approach of solving this problem that is we have to reverse the nodes in pairs, which is to swap the data of nodes of the Linked List in a group of two. However there are two possible ways to solve this problem based on implementation:
Solution idea
The idea here is to start with the head node and the node next to it and swap the data of both the node. After swapping we will move forward to the third node and will swap the data of third node with the fourth one and we will keep doing so until there is no node left or only one node left for swapping. Thus, all the nodes will be swapped in pairs.
Solution steps
Pseudo-code
ListNode swapInPairs(ListNode head)
{
if(head == NULL or head.next == NULL)
return head
ListNode first = head
ListNode second = head.next
while(first != NULL && second != NULL)
{
swapPairs(first.val, second.val)
first = second.next
second = first.next
}
return head
}
Complexity Analysis
Time Complexity = O(N), where N is the length of the Linked List.
Space Complexity = O(1)
Solution idea
The idea is to swap the data of the first pair and left the job of pairwise swapping of the remianing n-2 nodes on recursion. Here we are solving the problem of size n with the solution of smaller problems of size n-2.
Solution steps
Pseudo-code
void swapInPairs(ListNode head)
{
if(head == NULL || head.next == NULL)
return
swapPairs(head.val, head.next.val)
swapInPairs(head.next.next)
}
Complexity Analysis
Recurrence relation: T(N) = T(N-2) + O(1).
We can simply analyse this recurrence using the recursion tree method or other method.
Time Complexity = O(N), where N is the length of the Linked List.
Space Complexity = O(N) for the recursion call stack. (Think!)
Critical ideas to think!
Happy Coding! Enjoy Algorithms!!
AfterAcademy Tech
This is an interview problem asked by companies like Myntra, Visa, Oracle, Adobe, etc. The problem is to remove the duplicate nodes from the given Linked List so that only unique nodes(with unique data values) are present.

AfterAcademy Tech
Given a Linked List, your task is to find whether the given singly linked list is a palindrome or not. This is an interview problem asked in many companies like Amazon, Microsoft, Adobe, Flipkart and other big companies.

AfterAcademy Tech
Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists.

AfterAcademy Tech
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contains a single digit. Add the two numbers and return it as a linked list.
