Approach
We will maintain the following 3 pointers:
- current: We will use this pointer to traverse the LinkedList.
- prev: This will point to the previous pointer of the current node.
- nextOfCur: This will point to the next pointer of the current node.
At any node we will do the following:
- First, we will set the next of current to be prev i.e. current.next = prev.
- Then, we will set the next of prev to be the next of nextOfCur i.e. prev.next = nextOfCur.next.
- Then we will update the prev and current pointers.
Refer to the below implementation of the above approach.
public Node pairWiseSwap(Node head){
/*
If the linked list is
empty or there is only
one node in list
*/
if (head == null || head.next == null) {
return head;
}
// Initialize the previous and current pointers
Node prev = head;
Node current = head.next;
head = current;
// Traversing the list
while (true) {
Node nextOfCur = current.next;
/*
Updating the next
of the current
pointer to be prev.
*/
current.next = prev;
// If next is NULL or next is the last node
if (nextOfCur == null || nextOfCur.next == null) {
prev.next = nextOfCur;
break;
}
// Change next of previous to next next
prev.next = nextOfCur.next;
// Update the previous and current
prev = nextOfCur;
current = prev.next;
}
return head;
}
You can also try this code with Online Java Compiler
Run Code
Time Complexity: The time complexity for the above approach to pairwise swap the elements of the Linked List is O(N) (where ‘N’ is the number of nodes in the Linked List) because we are just iterating the Linked List once.
Space Complexity: The space complexity of the above approach to pairwise swap the elements of the Linked List is O(1) because we are using the constant auxiliary space.
Frequently Asked Questions
What are prev, current, and next pointing to when we pairwise swap the elements of the Linked List?
current: We will use this pointer to traverse the LinkedList.
prev: This will point to the previous pointer of the current node.
nextOfCur: This will point to the next pointer of the current node.
What is the time complexity for the approach we have used to pairwise swap the elements of the Linked List?
The time complexity for the approach to pairwise swap the elements of the Linked List is O(N) (where N is the number of nodes in the Linked List) because we are just iterating the Linked List once.
Is there any other method to solve this problem?
Yes, we can also solve this question by just swapping the data of adjacent nodes instead of changing the links of the nodes.
Conclusion
In this blog, we have covered the following things:
- We first discussed the approach to pairwise swap the elements of the Linked List.
-
Then we implemented the approach we discussed.
If you want to learn more about Linked List and want to practice some questions which require you to take your basic knowledge on Linked List a notch higher, then you can visit our Guided Path for Linked List. To practice this problem, you can visit Practice Problem.
Recommended Reading:
Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.
Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.
Cheers!