-
Notifications
You must be signed in to change notification settings - Fork 0
/
024.cpp
36 lines (35 loc) · 866 Bytes
/
024.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
void swapNode(ListNode *first)
{
ListNode *tmp1 = first -> next, *tmp2 = first->next->next, *tmp3=first->next->next->next;
first -> next = tmp2 ;
tmp2 -> next = tmp1;
tmp1 -> next = tmp3;
}
ListNode* swapPairs(ListNode* head) {
// 创建头节点
ListNode *res = new ListNode(0) ;
res->next = head;
head = res;
while(head != NULL)
{
if(head->next == NULL || head->next->next == NULL)
break;
else
{
swapNode(head);
head = head->next->next;
}
}
return res->next;
}
};