My java solution


  • 0
    C
        public class Solution {
        public ListNode swapPairs(ListNode head) {
    	if(head == null)return null;
        ListNode previous = head;
        ListNode latter = head.next;
    	//判断latter是否为空
        while((latter != null) &&(previous != null))
        {
        	int tmp = latter.val;
        	latter.val = previous.val;
        	previous.val = tmp;  
        	previous=latter.next;
        	if(previous != null)
        	latter=previous.next;
        }
        return head;
    

    }
    }


Log in to reply
 

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