mirror of
				https://git.wolves.top/wolves/leetcode.git
				synced 2025-11-04 09:16:32 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			28 lines
		
	
	
		
			501 B
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			28 lines
		
	
	
		
			501 B
		
	
	
	
		
			C++
		
	
	
	
	
	
#pragma once
 | 
						|
#include "lists.h"
 | 
						|
 | 
						|
class Q24 {
 | 
						|
public:
 | 
						|
    ListNode *swapPairs(ListNode *head) {
 | 
						|
        if (!head || !head->next) {
 | 
						|
            return head;
 | 
						|
        }
 | 
						|
        ListNode *p1;
 | 
						|
        ListNode *p2;
 | 
						|
        p1 = head;
 | 
						|
        p2 = head->next;
 | 
						|
 | 
						|
        while (!p2){
 | 
						|
            p1->next = p2->next;
 | 
						|
            p2->next = p1;
 | 
						|
 | 
						|
            if(!p1->next){
 | 
						|
                return head;
 | 
						|
            }
 | 
						|
 | 
						|
            p1 = p1->next;
 | 
						|
            p2 = p1->next;
 | 
						|
        }
 | 
						|
        return head;
 | 
						|
    }
 | 
						|
}; |