Skip to content

Commit bfdf75f

Browse files
committed
Added task 25.
1 parent 98375c1 commit bfdf75f

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package g0001_0100.s0025_reverse_nodes_in_k_group
2+
3+
// #Hard #Top_100_Liked_Questions #Linked_List #Recursion #Data_Structure_II_Day_13_Linked_List
4+
// #2022_04_26_Time_194_ms_(87.72%)_Space_35.7_MB_(100.00%)
5+
6+
import com_github_leetcode.ListNode
7+
8+
class Solution {
9+
fun reverseKGroup(head: ListNode?, k: Int): ListNode? {
10+
if (head?.next == null || k == 1) {
11+
return head
12+
}
13+
var j = 0
14+
var len = head
15+
// loop for checking the length of the linklist, if the linklist is less than k, then return
16+
// as it is.
17+
while (j < k) {
18+
if (len == null) {
19+
return head
20+
}
21+
len = len.next
22+
j++
23+
}
24+
// Reverse linked list logic applied here.
25+
var c = head
26+
var n: ListNode? = null
27+
var prev: ListNode? = null
28+
var i = 0
29+
// Traverse the while loop for K times to reverse the node in K groups.
30+
while (i != k) {
31+
n = c!!.next
32+
c.next = prev
33+
prev = c
34+
c = n
35+
i++
36+
}
37+
// C1 is pointing to 1st node of K group, which is now going to point to the next K group
38+
// linklist.
39+
// recursion, for futher remaining linked list.
40+
head.next = reverseKGroup(n, k)
41+
return prev
42+
}
43+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
25\. Reverse Nodes in k-Group
2+
3+
Hard
4+
5+
Given a linked list, reverse the nodes of a linked list _k_ at a time and return its modified list.
6+
7+
_k_ is a positive integer and is less than or equal to the length of the linked list. If the number of nodes is not a multiple of _k_ then left-out nodes, in the end, should remain as it is.
8+
9+
You may not alter the values in the list's nodes, only nodes themselves may be changed.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2020/10/03/reverse_ex1.jpg)
14+
15+
**Input:** head = [1,2,3,4,5], k = 2
16+
17+
**Output:** [2,1,4,3,5]
18+
19+
**Example 2:**
20+
21+
![](https://assets.leetcode.com/uploads/2020/10/03/reverse_ex2.jpg)
22+
23+
**Input:** head = [1,2,3,4,5], k = 3
24+
25+
**Output:** [3,2,1,4,5]
26+
27+
**Example 3:**
28+
29+
**Input:** head = [1,2,3,4,5], k = 1
30+
31+
**Output:** [1,2,3,4,5]
32+
33+
**Example 4:**
34+
35+
**Input:** head = [1], k = 1
36+
37+
**Output:** [1]
38+
39+
**Constraints:**
40+
41+
* The number of nodes in the list is in the range `sz`.
42+
* `1 <= sz <= 5000`
43+
* `0 <= Node.val <= 1000`
44+
* `1 <= k <= sz`
45+
46+
**Follow-up:** Can you solve the problem in O(1) extra memory space?
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0001_0100.s0025_reverse_nodes_in_k_group
2+
3+
import com_github_leetcode.ListNode
4+
import org.hamcrest.CoreMatchers.equalTo
5+
import org.hamcrest.MatcherAssert.assertThat
6+
import org.junit.jupiter.api.Test
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun reverseKGroup() {
11+
val head = ListNode()
12+
val node1 = ListNode()
13+
node1.`val` = 1
14+
head.next = node1
15+
val node2 = ListNode()
16+
node2.`val` = 2
17+
node1.next = node2
18+
val node3 = ListNode()
19+
node3.`val` = 3
20+
node2.next = node3
21+
val node4 = ListNode()
22+
node4.`val` = 4
23+
node3.next = node4
24+
val node5 = ListNode()
25+
node5.`val` = 5
26+
node4.next = node5
27+
assertThat(Solution().reverseKGroup(node1, 2).toString(), equalTo("2, 1, 4, 3, 5"))
28+
}
29+
}

0 commit comments

Comments
 (0)