-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path24.cpp
33 lines (30 loc) · 771 Bytes
/
24.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
/**
* 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 *prev = nullptr,*curr = head;
while(curr && curr->next){
if(prev){
prev->next = curr->next;
curr->next = curr->next->next;
prev->next->next = curr;
}
else{
prev = curr->next;
curr->next = prev->next;
prev->next = curr;
head = prev;
}
prev = curr;
curr = curr->next;
}
return head;
}
};