There was an error while loading. Please reload this page.
1 parent b24e68d commit e37f97eCopy full SHA for e37f97e
C++/Swap-nodes-in-pairs.cpp
@@ -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