Swap Nodes in Pairs


  • 0
    Z

    if(head==null||head.next==null){
    return head;
    }
    ListNode dummy=new ListNode(-1);
    dummy.next=head;
    ListNode pivot=dummy;
    ListNode tail=dummy;
    pivot=pivot.next;
    while(pivot!=null){
    ListNode temp=pivot.next;
    if(pivot.next==null){
    break;
    }
    pivot.next=pivot.next.next;
    temp.next=null;
    temp.next=pivot;
    tail.next=temp;
    tail=tail.next.next;
    pivot=pivot.next;
    }
    return dummy.next;


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.