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;
}

12 thoughts on “LeetCode – Swap Nodes in Pairs (Java)”


  1. Might be more a more clearer solution:

    Node swapPairs(Node head)
    {
    Node headHelper = new Node();
    headHelper.next = head;

    /* prev node before next pair */
    Node prev = headTemp;

    /* Has next pair */
    while (prev.next != null && prev.next.next != null)
    {
    /* Start of pair */
    Node curr = prev.next;

    /* Remember the next Pair */
    Node nextPair = curr.next.next;

    /* Swap */
    prev.next = curr.next;
    prev.next.next = curr;
    curr.next = nextPair;

    /* Iterate to next */
    prev = curr;
    }

    return headHelper.next;
    }

  2. 100% on leet code:

    class Solution {
    public ListNode swapPairs(ListNode head) {

    ListNode helper = new ListNode(0);
    helper.next = head;

    ListNode previous = helper;
    ListNode current = head;

    while(current != null){
    ListNode next = current.next;
    if(next!=null){
    current.next = next.next;
    previous.next = next;
    next.next = current;
    previous = previous.next.next;
    current = current.next;
    }else{
    current = current.next;
    }
    }

    return helper.next;
    }
    }

  3. in while loop : p.next != null is the same head == null in if condition ahead


  4. 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;
    }
    }

  5. 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;
    }
    };

  6. 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;

  7. 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;
    }

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

Leave a Comment