-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#23.cc
More file actions
34 lines (34 loc) · 876 Bytes
/
Copy pathLeetCode#23.cc
File metadata and controls
34 lines (34 loc) · 876 Bytes
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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
private:
ListNode * dfs(ListNode* node, int num){
if(node==NULL) return NULL;
ListNode* t = dfs(node->next,num+1);
node->next = t;
if(num&1){
if(node->next==NULL) return node;
else{
ListNode* tmp = node->next;
node->next=tmp->next;
tmp->next = node;
return tmp;
}
}
else return node;
}
public:
ListNode *swapPairs(ListNode *head) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(head==NULL) return NULL;
if(head->next==NULL) return head;
return dfs(head,1);
}
};