# DAY 15
0

## 題目:

https://leetcode.com/problems/swap-nodes-in-pairs/

## 解題思路:

### C版本:

``````struct ListNode* swapPairs(struct ListNode* head) {

struct ListNode *p2 = head -> next;
struct ListNode *result = p2;
struct ListNode *temp = NULL ;
while (p1 != NULL && p2 != NULL )
{
p1 -> next = p2 -> next;
p2 -> next = p1;

if (temp != NULL )
temp -> next = p2;
temp = p1;
if (p1 -> next != NULL && p1 -> next -> next != NULL )
{
p1 = p1 -> next;
p2 = p1 -> next;
}
else
break ;
}
return result;
}
``````

### Javascript版本:

``````var swapPairs = function(head) {
var p1 = new ListNode(0);
var p2 = new ListNode(0);
var result = new ListNode(0);
var temp = new ListNode(0);

result = p2;
temp = null;

while (p1 != null && p2 != null )
{
p1.next = p2.next;
p2.next = p1;

if (temp != null )
temp.next = p2;
temp = p1;
if (p1.next != null && p1.next.next != null )
{
p1 = p1.next;
p2 = p1.next;
}
else
break ;
}
return result;
};
``````

## 程式Github分享:

https://github.com/SIAOYUCHEN/leetcode

## 本日分享:

Accept yourself and experience the perfection in imperfections in life