Pairwise swap elements of a linked list | Practice | GeeksforGeeks

Given a singly linked list of size N. The task is to swap elements in the linked list pairwise.
For example, if the input list is 1 2 3 4, the resulting list after swaps will be 2 1 4 3.
Note: You need to swap the nodes, not only the data. If only data is swapped then driver will print -1.
Example 1:
Input:
LinkedList: 1->2->2->4->5->6->7->8
Output: 2 1 4 2 6 5 8 7
Explanation: After swapping each pair
considering (1,2), (2, 4), (5, 6).. so
on as pairs, we get 2, 1, 4, 2, 6, 5,
8, 7 as a new linked list.

 
Example 1:
Input:
LinkedList: 1->3->4->7->9->10->1
Output: 3 1 7 4 10 9 1
Explanation: After swapping each pair
considering (1,3), (4, 7), (9, 10).. so
on as pairs, we get 3, 1, 7, 4, 10, 9,
1 as a new linked list.
Your Task:
The task is to complete the function pairWiseSwap() which takes the head node as the only argument and returns the head of modified linked list.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 103
0
abhaybaranwal41 week ago
 public Node pairwiseSwap(Node head)   {       // code here       if(head==null|| head.next==null)       {           return head;       }       Node curr=head.next.next;       Node prev=head;       head=head.next;       head.next=prev;       while(curr!=null && curr.next!=null)       {           prev.next=curr.next;           prev=curr;           Node next=curr.next.next;           curr.next.next=curr;           curr=next;                             }       prev.next=curr;       return head;     }
0
tthakare732 weeks ago
//Java Solution -> TC -> 1.57
class Solution {
    public Node pairwiseSwap(Node head){
        // Veriables
        Node forword  = head.next, backword = head, tempBack = null, tempForword = null, pre = null;
        while(backword != null && forword != null){
            tempForword = forword;
            tempBack = backword;
    
            tempBack.next = tempForword.next;
            tempForword.next = tempBack;
            
            if(backword == head) head = tempForword;
            
            backword = tempBack.next;
            if(backword != null && backword.next != null) {
                forword = backword.next;
                tempBack.next = forword;
            } else {
                forword = null;
            }
        }
        return head;
        
    }
}
0
harshpandeyalfa22 weeks ago
C++
(0.1sec)
    Node* pairWiseSwap(struct Node* head)    {       if(head==0 || head->next==0)        return head;       Node* it1=head;       Node* it2=head->next;       it1->next=it2->next;       it2->next=it1;       head=it2;       Node* temp;       temp=it1;       it1=it2;       it2=temp;       Node*i=it2;       if(it2->next==0)            return head;       if(it2->next->next==0)            return head;       it1=it2->next;       it2=it2->next->next;       while(it1 && it2)       {       it1->next=it2->next;       it2->next=it1;       temp=it1;       it1=it2;       it2=temp;       i->next=it1;       i=it2;       if(it2->next==0)            return head;       if(it2->next->next==0)            return head;       it1=it2->next;       it2=it2->next->next;       }       return head;       // The task is to complete this method   }
0
hharshit81183 weeks ago
Time Complexity: O(N).Auxiliary Space: O(1).
Node* pairWiseSwap(struct Node* head)    {       Node *curr = head;       bool visit = true;       Node* pre_first = curr;       while(curr != NULL){           Node* pre_last = curr;           Node* t1 = NULL, *t2 = NULL;           for(int i = 0; i< 2 && curr != NULL; i++){               t2 = curr->next;               curr->next = t1;               t1 = curr;               curr = t2;           }           if(visit){               head = t1;               visit = false;           }           else{               pre_first->next = t1;               pre_first = pre_last;           }       }       return head;   }
0
sagrikasoni3 weeks ago
class Solution {

    public Node pairwiseSwap(Node head)
    {
       int c =0;
    
       Node pre = null, curr = head, f =null;
       while(curr!=null && c<2){
           f = curr.next;
           curr.next = pre;
           pre = curr;
           curr = f;
           c++;
       }
       if(f!=null){
           head.next = pairwiseSwap(f);
       }
    
     return pre;
    }
}
+3
as0042301 month ago
In C++;
Node* pairWiseSwap(struct Node* head)    {       Node *first = head;       Node *prev, *second, *next;              while(first && first->next)       {           second = first->next;           next = second->next;                      if(first == head)               head = first->next;           else               prev->next = second;                          second->next = first;           first->next = next;                      prev = first;           first = next;       }       return head;   }
0
as0042301 month ago
In C++;
//Using Recursion
Node* pairWiseSwap(struct Node* head)    {       Node *cur = head;       Node *next = NULL;       Node *prev = NULL;       int count = 0;              while(cur && count < 2)       {           next = cur->next;           cur->next = prev;           prev = cur;           cur = next;           count++;       }              if(next != NULL)   head->next = pairWiseSwap(next);       return prev;   }
-1
sparhibsp20181 month ago
simple and understandable code using c++
Node* pairWiseSwap(struct Node* head)    {       if(head->next==NULL)       {           return head;       }       Node* first=head;       Node* second=head->next;       head=head->next;       Node* pre;       while ( second!=NULL && second->next!=NULL )       {           Node* store=second->next;           first->next=second->next->next;           second->next=first;           pre=first;           first=store;           second=first->next;
       }       if(second!=NULL)       {           second->next=first;           first->next=NULL;       }       else       {           pre->next=first;       }          return head;      }
0
tapanmanu20001 month ago
Node* pairWiseSwap(struct Node* head) 
    {
        // The task is to complete this method
        if(head == NULL) return head;
        if(head->next == NULL) return head;
        
        Node* nextNode = pairWiseSwap(head->next->next);
        Node* pair = head->next;
        pair->next = head;
        head->next = nextNode;
        return pair;
        
    }
0
rafi19981 month ago
// Java Solution
public Node pairwiseSwap(Node head)
    {
        if(head.next==null)
            return head;
            
        Node t=head;
        Node temp=t.next.next;
        head=t.next;
        
        while(temp!=null && temp.next!=null){
            t.next.next=t;
            t.next=temp.next;
            t=temp;
            temp=t.next.next;
        }
        t.next.next=t;
        t.next=temp;
        
        return head;
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.