# Reverse Nodes in k-Group LeetCode Solution

Here, We see Reverse Nodes in k-Group problem Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc., with different approaches.

## Problem Statement ->

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 the nodes themselves may be changed.

```Example 1: (fig-1)
Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]```
```Example 2: (fig-2)
Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]```

## Reverse Nodes in k-Group Leetcode Solution C++ ->

``````class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
for(int i=0;i<k;i++){
temp=temp->next;
}

ListNode *prev=NULL;
ListNode *nex1=NULL;
for(int i=0;i<k;i++){
nex1=temp->next;
temp->next=prev;
prev=temp;
temp=nex1;
}
if(nex1!=NULL)
return prev;
}
};
```Code language: C++ (cpp)```

## Reverse Nodes in k-Group Leetcode Solution Java ->

``````class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode prev = null;
int count = 0;
ListNode next = null;

//reverse blindly
while (current != null && count < k) {
next = current.next;
current.next = prev;
prev = current;
current = next;
count++;
}

//restoring if count lesser
if (count < k) {
current = prev;
prev = null;
next = null;

while (count != 0) {
count--;
next = current.next;
current.next = prev;
prev = current;
current = next;
}
}
if (next != null) {
}
return prev;
}
}
```Code language: Java (java)```

## Reverse Nodes in k-Group Leetcode Solution JavaScript ->

``````var reverseKGroup = function(head, k) {

function reverseAGroup(start) {
let temp_k = k;
let curr = start;
while(curr && temp_k-- > 0) {
curr = curr.next;
}
if(temp_k > 0) {
return start;
}
const groupTail = start,

if(curr) {
groupTail.next = reverseAGroup(curr);
}
}
function reverse(root, nextGroup) {
let curr = root,
next = null,
prev = null;

while(curr && curr !== nextGroup) {
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
return prev;
}
};
```Code language: JavaScript (javascript)```

## Reverse Nodes in k-Group Leetcode Solution Python ->

``````class Solution(object):
while node:
l += 1
node = node.next
if k <= 1 or l < k:
for _ in xrange(k):
nxt = cur.next
cur.next = node
node = cur
cur = nxt
return node
```Code language: Python (python)```

Given an integer num, repeatedly add all its digits until the result has only one digit,…

#### Remove Duplicates from Sorted Array LeetCode Solution

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once….

#### Wildcard Matching LeetCode Solution

Given an input string (s) and a pattern (p), implement wildcard pattern matching with support…