LeetCode – Swap Nodes in Pairs (Java)

Given a linked list, swap every two adjacent nodes and return its head.

For example, given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

Java Solution 1

Use two template variable to track the previous and next node of each pair.

public ListNode swapPairs(ListNode head) {
    if(head == null || head.next == null)   
        return head;
 
    ListNode h = new ListNode(0);
    h.next = head;
    ListNode p = h;
 
    while(p.next != null && p.next.next != null){
        //use t1 to track first node
        ListNode t1 = p;
        p = p.next;
        t1.next = p.next;
 
        //use t2 to track next node of the pair
        ListNode t2 = p.next.next;
        p.next.next = p;
        p.next = t2;
    }
 
    return h.next;
}

Java Solution 2

Each time I do the same problem I often get the different solutions. Here is another way of writing this solution.

public ListNode swapPairs(ListNode head) {
    if(head==null || head.next==null)
        return head;
 
    //a fake head
    ListNode h = new ListNode(0);
    h.next = head;
 
    ListNode p1 = head;
    ListNode p2 = head.next;
 
    ListNode pre = h;
    while(p1!=null && p2!=null){
        pre.next = p2;
 
        ListNode t = p2.next;
        p2.next = p1;
        pre = p1;
        p1.next = t;
 
        p1 = p1.next;
 
        if(t!=null)
            p2 = t.next;
    }
 
    return h.next;
}
Category >> Algorithms >> Interview  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • Chungho Song


    public static ListNode swapPairs(ListNode head) {
    if(head == null || head.next == null)
    return head;
    else{
    // swap the two
    ListNode newHead = head.next;
    head.next = swapPairs(head.next.next);
    newHead.next = head;
    return newHead;
    }
    }

  • Abhishek Jaiswal

    class Solution {
    public:
    ListNode* swapPairs(ListNode* head) {

    if(!head || !head->next) return head;

    ListNode* send = head->next->next;
    ListNode* temp = head->next;

    temp->next = head;

    head->next = swapPairs(send);

    return temp;
    }
    };

  • Aaron Ke

    hey dude, p.next.next = p; should change to t1.next.next=p;

  • ishan73

    if(head==null || head.next==null){

    return head;

    }

    ListNode slow=head.next;

    head.next=head.next.next;

    slow.next=head;

    head=slow;

    ListNode parent=slow.next;

    slow=slow.next.next;

    while(slow!=null && slow.next!=null){

    ListNode temp=slow.next;

    slow.next=slow.next.next;

    temp.next=slow;

    parent.next=temp;

    parent=parent.next.next;

    slow=slow.next;

    }

    return head;

  • Anonymous

    public ListNode swapPairs(ListNode head) {
    if(head == null || head.next == null){
    return head;
    }

    ListNode next = swapPairs(head.next.next);
    ListNode temp = head.next;
    head.next = next;
    temp.next = head;
    return temp;
    }

  • Sushant Raj

    For Input–1211—>12—>13—>124—>121—>125—>
    output—>1211—>124—>13—>125—>121—>

  • Sushant Raj

    This code is not working…It removes the alternate element from the list..