-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.c
66 lines (58 loc) · 1.63 KB
/
24.c
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Swap Nodes in Pairs
// Given 1->2->3->4
// Return 2->1->4->3
/* My solution (0 ms!?) */
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* swapPairs(struct ListNode* head) {
if (!head) return NULL; // 0 node
if (!(head->next)) return head; // 1 node
int flag = 0;
struct ListNode* dummy_head = malloc(sizeof(struct ListNode));
struct ListNode* pfirst; // pointer of the first node of swap
struct ListNode* pnext; // pointer of the third node
struct ListNode* current;
dummy_head->next = head;
current = dummy_head;
while (current->next) {
if (flag) { // current = second node of swap
flag = 0;
pnext = current->next;
current->next = pnext->next;
pfirst->next = pnext;
pnext->next = current;
}
else { // current = first node of swap
flag = 1;
pfirst = current;
current = current->next;
}
}
return dummy_head->next;
}
/* ====A c++ solution from jexxllz==== */
/* Brilliant recursive solution */
/**
* 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(head==nullptr||head->next==nullptr) return head;
ListNode* left=head;
ListNode* right=head->next;
left->next=swapPairs(right->next);
right->next=left;
return right;
}
};