My accepted C++ solution without recursion


  • 0
    C
    class Solution {
    public:
    	ListNode* swapPairs(ListNode* head) {
    		ListNode temp(0);
    		temp.next = head;
    		ListNode *last = &temp, *p1, *p2;
    		while ((p1 = last->next) && (p2 = p1->next))
    		{
    			p1->next = p2->next;
    			last->next = p2;
    			p2->next = p1;
    			last = p1;
    		}
    		return temp.next;
    	}
    };

Log in to reply
 

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