
g0001_0100.s0025_reverse_nodes_in_k_group.Solution.cs Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
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;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy