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;
}
};
代码: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;
}
};
相关文章推荐
- leetcode 24.Swap Nodes in Pairs
- LeetCode24-Swap Nodes in Pairs
- 【leetcode】【单链表】【24】Swap Nodes in Pairs
- LeetCode-24-Swap-Nodes-in-Pairs 链表递归水题
- LeetCode(24)Swap Nodes in Pairs
- [Leetcode]24. Swap Nodes in Pairs
- leetcode 24. Swap Nodes in Pairs
- [leetcode 24]Swap Nodes in Pairs-----成对翻转链表中的节点
- leetcode 24: Swap Nodes in Pairs
- [leetcode 24]Swap Nodes in Pairs
- Leetcode 24 Swap Nodes in Pairs
- leetcode 24. Swap Nodes in Pairs
- LeetCode 24. Swap Nodes in Pairs
- 【leetcode】【单链表】【24】Swap Nodes in Pairs
- leetcode 24. Swap Nodes in Pairs
- LeetCode 24 Swap Nodes in Pairs (链表 递归&&非递归)
- 【leetcode】24. Swap Nodes in Pairs
- Leetcode 24. Swap Nodes in Pairs
- LeetCode 24. Swap Nodes in Pairs
- leetcode 24 -- Swap Nodes in Pairs