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

leetcode --24. Swap Nodes in Pairs

2017-08-16 19:47 429 查看
题目:https://leetcode.com/problems/swap-nodes-in-pairs/description/

代码:class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(head == NULL)
return NULL;
ListNode *dummy = new ListNode(-1);
dummy->next = head;
ListNode *p=dummy,*q=head;
while(q!=NULL&&q->next!=NULL){
ListNode *temp = q->next->next;
q->next->next = q;
p->next = q->next;
q->next = temp;
p = q;
q = q ->next;
}

return dummy->next;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: