My simple 2ms solution


  • 0
    N
    class Solution {
    public:
        ListNode *swapPairs(ListNode *head) {
            if (!head||!head->next)
    			return head;
    		ListNode** cur = &head;
    		while ((*cur)&&(*cur)->next){
    			ListNode* temp = (*cur)->next;
    			(*cur)->next = (*cur)->next->next;
    			temp->next=*cur;
    			*cur = temp;
    			cur = &(*cur)->next->next;
    		}
    		return head;
        }
    };

Log in to reply
 

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