您的位置:首页 > Web前端 > Node.js

LeetCode-Swap Nodes in Pairs

2014-04-20 21:23 507 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Algorithm leetcode List