We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent ca04bda commit 7e68edeCopy full SHA for 7e68ede
Algorithms Basics/Chapter 2. Linked List/24. Swap Nodes in Pairs.cs
@@ -1,3 +1,4 @@
1
+// iteration
2
public class Solution {
3
public ListNode SwapPairs(ListNode head) {
4
if (head == null || head.next == null) return head;
@@ -20,4 +21,15 @@ public ListNode SwapPairs(ListNode head) {
20
21
}
22
return dummy.next;
23
24
+}
25
+
26
+// recursion
27
+public class Solution2 {
28
+ public ListNode SwapPairs(ListNode head) {
29
+ if (head == null || head.next == null) return head;
30
+ ListNode n = head.next;
31
+ head.next = SwapPairs(n.next);
32
+ n.next = head;
33
+ return n;
34
+ }
35
0 commit comments