###### Swap Nodes in Pairs

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* h1 = head, *parent = NULL, *h3 = NULL;
while(h1!=NULL && h1->next!=NULL)
{
h3 = h1->next->next;

//swap h1 & h1->next
if(parent == NULL)
head = h1->next;
else
parent->next = h1->next;

h1->next->next = h1;
h1->next = h3;

parent = h1;
h1 = h3;
}
return head;
}
};

/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if(NULL == head || NULL == head->next)
return head;
ListNode* h1 = head;
head = head->next;
ListNode* h3 = head->next;
head->next = h1;
h1->next = h3;

ListNode* parent = h1;
h1 = h3;

while(h1!=NULL)
{
if(h1->next == NULL)
break;
h3 = h1->next->next;

//swap h1 & h2
parent->next = h1->next;
h1->next->next = h1;
h1->next = h3;

parent = h1;
h1 = h3;
}
return head;
}
};

#### LeetCode 24 Swap Nodes in Pairs (C,C++,Java,Python)

2015-05-11 16:37:22

#### (Java)LeetCode-24. Swap Nodes in Pairs

2016-06-02 16:53:15

#### Swap Nodes in Pairs -- LeetCode

2014-02-26 01:37:59

#### 24. Swap Nodes in Pairs [easy] (Python)

2016-05-29 15:09:02

#### [LeetCode] 024. Swap Nodes in Pairs (Medium) (C++/Python)

2015-03-05 14:39:27

#### LeetCode24. Swap Nodes in Pairs简单到一次性通过

2016-02-02 21:01:22

#### 24. Swap Nodes in Pairs -leetcode-java

2016-05-26 18:54:52

#### leetcode Swap Nodes in Pairs java实现

2014-03-07 12:17:26

#### LeetCode 24 Swap Nodes in Pairs（交换序列中的结点）（Linked List）

2015-11-12 18:43:57

#### LeetCode 24. Swap Nodes in Pairs 解题报告

2016-02-20 17:31:56

## 不良信息举报

Swap Nodes in Pairs