Share my Java Code using dummy


  • 0
    Y
    public class Solution {
    public ListNode swapPairs(ListNode head) {
        ListNode dummy=new ListNode(1),n=head,n2=null,i=dummy;
    	dummy.next=head;
    	while(n!=null&&n.next!=null){
    		n2=n.next.next;
    		ListNode temp=n.next;
    		i.next=temp;
    		temp.next=n;
    		n.next=n2;
    		n=n2;
    		i=i.next.next;
    	}
    	return dummy.next;
    }
    

    }


Log in to reply
 

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