forked from LeetCode-in-Net/LeetCode-in-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.cs
56 lines (53 loc) · 1.67 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
48
49
50
51
52
53
54
55
56
namespace LeetCodeNet.G0001_0100.S0025_reverse_nodes_in_k_group {
// #Hard #Top_100_Liked_Questions #Linked_List #Recursion #Data_Structure_II_Day_13_Linked_List
// #Udemy_Linked_List #Big_O_Time_O(n)_Space_O(k)
// #2023_12_26_Time_75_ms_(86.97%)_Space_43.9_MB_(5.81%)
using LeetCodeNet.Com_github_leetcode;
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode ReverseKGroup(ListNode head, int k) {
if (head == null || head.next == null || k == 1) {
return head;
}
int j = 0;
ListNode len = head;
// loop for checking the length of the linked list, if the linked list is less than k, then return
// as it is.
while (j < k) {
if (len == null) {
return head;
}
len = len.next;
j++;
}
// Reverse linked list logic applied here.
ListNode c = head;
ListNode n = null;
ListNode prev = null;
int i = 0;
// Traverse the while loop for K times to reverse the nodes in K groups.
while (i != k) {
n = c.next;
c.next = prev;
prev = c;
c = n;
i++;
}
// `head` is pointing to the 1st node of the K group, which is now going to point to the next K group
// linked list.
// Recursion for further remaining linked list.
head.next = ReverseKGroup(n, k);
return prev;
}
}
}