Smallest and Easiest Java Solution in O(1) space and O(n) time.


  • 0
    D
    class Solution
    {
        public ListNode swapPairs(ListNode head)
        {
            ListNode t = head;
            ListNode res = t;
            while(t!=null && t.next!=null)
            {
            	int a = t.val;
            	int b = t.next.val;
            	t.val = b;
            	t.next.val = a;
            	t = t.next.next;
            }
            return res;
        }
    }
    

Log in to reply
 

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