forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cs
47 lines (42 loc) · 1.21 KB
/
Solution.cs
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
namespace LeetCodeNet.G0001_0100.S0024_swap_nodes_in_pairs {
// #Medium #Top_100_Liked_Questions #Linked_List #Recursion #Data_Structure_II_Day_12_Linked_List
// #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(1)
// #2023_12_26_Time_58_ms_(98.51%)_Space_40_MB_(9.14%)
using LeetCodeNet.Com_github_leetcode;
public class Solution {
public ListNode SwapPairs(ListNode head) {
if (head == null) {
return null;
}
int len = GetLength(head);
return Reverse(head, len);
}
private int GetLength(ListNode curr) {
int cnt = 0;
while (curr != null) {
cnt++;
curr = curr.next;
}
return cnt;
}
// Recursive function to reverse in groups
private ListNode Reverse(ListNode head, int len) {
// base case
if (len < 2) {
return head;
}
ListNode curr = head;
ListNode prev = null;
ListNode next;
for (int i = 0; i < 2; i++) {
// reverse linked list code
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
head.next = Reverse(curr, len - 2);
return prev;
}
}
}