# Reverse Nodes in k-Group Leetcode Solution

#### ByBrokenprogrammers

Dec 7, 2022

In this post, you will know how to solve the Reverse Nodes in k-Group Leetcode Solution problem of Leetcode. This Leetcode problem is done in many programming languages like C++, Java, and Python.

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Leetcode by yourself. If you find any difficulty after trying several times, then you can look for solutions.

## Problem

Given the `head` of a linked list, reverse the nodes of the list `k` at a time, and return the modified list.

`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.

You may not alter the values in the list’s nodes, only nodes themselves may be changed.

Example 1:

```Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
```

Example 2:

```Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]
```

Constraints:

• The number of nodes in the list is `n`.
• `1 <= k <= n <= 5000`
• `0 <= Node.val <= 1000`

### Reverse Nodes in k-Group Leetcode Solutions in Python

```# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
def reverseKGroup(self, head: Optional[ListNode], k: int) -> Optional[ListNode]:
for _ in range(k):
if not b:
return a
b = b.next

a.next = self.reverseKGroup(b, k)

def reverse(self, a, b):
pre = None
cur = a
nxt = a
while cur != b:
nxt = cur.next
cur.next = pre
pre = cur
cur = nxt

return pre```

### Reverse Nodes in k-Group Leetcode Solutions in CPP

```/**
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode() : val(0), next(nullptr) {}
*     ListNode(int x) : val(x), next(nullptr) {}
*     ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode *dummy=new ListNode();
ListNode *cur=dummy,*nex=dummy,*pre=dummy;
int count=0;
while(cur->next!=NULL){
cur=cur->next;
count++;
}
while(count>=k){
cur=pre->next;
nex=cur->next;
for(int i=1;i<k;i++){
cur->next=nex->next;
nex->next=pre->next;
pre->next=nex;
nex=cur->next;
}
pre=cur;
count-=k;
}
return dummy->next;
}
};```

### Reverse Nodes in k-Group Leetcode Solutions in Java

```/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode() {}
*     ListNode(int val) { this.val = val; }
*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {

int len = 0;
while(curr != null){
curr = curr.next;
len++;
}
return len;
}

private ListNode th = null;
private ListNode tt = null;

if(th == null){
th = node;
tt = node;
}
else{
node.next = th;
th = node;
}
}

public ListNode reverseKGroup(ListNode head, int k) {

ListNode oh = null;
ListNode ot = null;

int len = length(curr);

while(len >= k){
int tempK = k;

while(tempK-- > 0){
ListNode forw = curr.next;
curr.next = null;
curr = forw;
}

if(oh == null){
oh = th;
ot = tt;
}else{
ot.next = th;
ot = tt;
}

th = null;
tt = null;
len -= k;
}

ot.next = curr;
return oh;
}
}```

Note: This problem Reverse Nodes in k-Group is generated by Leetcode but the solution is provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.