LeetCode-Swap Nodes in Pairs

#include <iostream>
using namespace std;

struct ListNode
{
    int val;
    ListNode *next;
};
void Swap(int *a,int *b)
{
    int temp=*a;
    *a=*b;
    *b=temp;
}
ListNode *swapPairs(ListNode *head)
{
    if(head==NULL)
        return NULL;
    if(head->next==NULL)
        return head;
    
    ListNode *node=head;
    while(node->next!=NULL)
    {
        Swap(&node->val,&node->next->val);
        
        if(node->next->next!=NULL)
            node=node->next->next;
        else
            break;
    }
    return head;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章