My java solution2


  • 0
    C
     public class Solution {
      public ListNode swapPairs(ListNode head) {
        if(head == null)return null;
        if(head.next == null)return head;
        
        ListNode tmp = new ListNode(0);
        ListNode first = head;
        ListNode second = head.next;
        
        if(head.next.next == null)
            {
                second.next=head;
                head.next=null;
                return second;
            }
        
        ListNode current = tmp;    
        
        while(first!= null && first.next != null )
        {   
            second=first.next;
        	first.next = second.next;
        	current.next=second;
        	current.next.next=first;
        	current=first;
        	first=first.next;
        	
        }
        return tmp.next;
    }
    

    }


Log in to reply
 

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