Skip to content

Commit e37f97e

Browse files
authored
Create Swap-nodes-in-pairs.cpp (#200)
* Create Swap-nodes-in-pairs.cpp * Update Swap-nodes-in-pairs.cpp
1 parent b24e68d commit e37f97e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

C++/Swap-nodes-in-pairs.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
ListNode* swapPairs(ListNode* head) {
14+
if(head==NULL || head->next==NULL)
15+
return head;
16+
ListNode* p=head->next;
17+
head->next=swapPairs(head->next->next);
18+
p->next=head;
19+
return p;
20+
}
21+
};
22+
23+
// Time Complexity: O(N)
24+
// Space Complexity: O(1)

0 commit comments

Comments
 (0)