Swap Kth node from beginning with Kth node from end in a Doubly Linked List
Prerequisites: Doubly Linked List
Given a doubly-linked list, the task is to swap Kth node from the beginning with Kth node from the ending.
Note: Please note here the nodes are swapped and not the data in the nodes.
Input: DLL = 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6, K = 3
Output: 1 2 4 3 5 6
Third node from the beginning(3) is swapped with third node from the ending(4).
Input: DLL = 1 <-> 2 <-> 3 <-> 4 <-> 5, K = 1
Output: 5 2 3 4 1
Approach: The idea is to traverse to the Kth element from the beginning and Kth node from the ending and change the previous and next pointers. Let K1 be the Kth node from beginning and K2 be Kth node from ending. Then:
- The previous node to K2 has to be changed to the previous node of K1.
- The next node to K2 has to be changed to the next node of K1.
- The previous node to K1 has to be changed to the previous node of K2.
- The next node to K1 has to be changed to the next node of K2.
Below is the implementation of the above approach:
1 4 3 2 5
Time Complexity: O(K), As we are traversing till Kth element from tail and head of the list and then changing the links which is a O(1) operation.
Auxiliary Space: O(1), As constant extra space is used.
Method 2: Without swapping the elements and without using temporary node.
Approach: There are 3 cases in order to swap the nodes.
- Swapping the first and the last nodes (k = 1)
- Swapping the ordinary Kth node from the beginning and Kth node from the end.
- Swapping middle nodes
Case 1: Swap first and last nodes (k = 1)
- Make the list as a circular linked list
- Change the previous pointer of the first node to the last but one node (20 in example figure)
- Change the next pointer of last but one node to the last node. In this case it will be 60.
- After swapping, make the head as the first node.
Consider p and q are the nodes which are to be swapped, head = q; //change head pointer to point to head node last = p; //change last pointer to point to last node
Case 2: Swapping the ordinary Kth node from the beginning and Kth node from the end.
- Let us consider K = 2. So the nodes to be swapped or interchanged are 20 and 50 as show in the figure.
- Make both the first and next pointers of the nodes which are to be swapped to point to the previous nodes. To do this, we need to change the links of the previous nodes to point to the node which is after the node to be swapped.
Consider the nodes to be swapped are p and q: //Change the link of the next pointer of the previous node to point to //the next node of to be swapped node. q.first.next = q.next; p.first.next = p.next; // Same procedure for the other node //Make sure to change the previous/first pointer of the next node to //point to the previous of to be swapped node. q.next.first = q.first; p.next.first = p.first; //Both the first and next pointers points to the previous node as shown in the below figure. q.next = q.first; p.next = p.first;
3. Interchange the pointers of one node to be swapped nodes with the other to be swapped node. (step 3 denotes the figure after interchanging).
4. Make the required changes in the links in order to make it as a complete list.
Case 3: Swapping the middle nodes
- This case is same as the case 2 and the only change is, the nodes which are to be swapped are middle nodes. So both of them are together (side-by-side).
- Consider p is the first node to be swapped and q is the second node to be swapped.
- Point the next pointer of previous node of p to the next node of q. This step is done to omit p and q nodes.
- In the same way, point the first pointer of next node of q to the previous node of q.
- Change the links of p and q so that both the nodes points to the previous node of p (step2 in the below figure).
- Make the links of p and q accordingly to make the nodes swap their positions.
Before swapping: 10<->20<->30<->40<->50<->60<->NULL After swapping nodes for k = 1: 60<->20<->30<->40<->50<->10<->NULL After swapping nodes for k = 2: 60<->50<->30<->40<->20<->10<->NULL After swapping nodes for k = 3 (middle): 60<->50<->40<->30<->20<->10<->NULL
Time complexity: O(N) where N is number of nodes in linked list
Auxiliary Space: O(1)
Please Login to comment...