# 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++ ->

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}
```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)```

#### Rotate Image LeetCode Solution

You are given an n x n 2D matrix representing an image, rotate the image…

#### Median of Two Sorted Arrays LeetCode Solution

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays. The overall run time…

#### Add Two Numbers LeetCode Solution

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order,…

#### Permutations LeetCode Solution

Given an array nums of distinct integers, return all the possible permutations. You can return…

#### Contains Duplicate LeetCode Solution

Given an integer array nums, return true if any value appears at least twice in the array, and return false if every…