simple java solution


  • 0
    K
    public ListNode swapPairs(ListNode head) {
            if(head==null)return null;
            int count=1;
            ListNode record=head;
            while (head.next!=null){
                if(count%2==1)swap(head,head.next);
                head=head.next;
                count++;
            }
            return record;
        }
        public static void swap(ListNode node1, ListNode node2){
            int tmp=node1.val;
            node1.val=node2.val;
            node2.val=tmp;
        }
    

Log in to reply
 

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