20 lines of C# code beating 96%


  • 0
    A
            public ListNode SwapPairs(ListNode head)
            {
                ListNode curr = head;
                int t1, t2;
    
                while (curr != null && curr.next != null)
                {
                    t1 = curr.val;
                    t2 = curr.next.val;
    
                    curr.val = t2;
                    curr.next.val = t1;
    
                    if (curr.next.next != null)
                        curr = curr.next.next;
                    else
                        break;
                }
                return head;
            }
    

Log in to reply
 

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