# Reverse Nodes In K-Group Program Solution

We at gradjobopenings.com provide free job alerts of freshers job drives. In this website we list on campus job openings for freshers and off campus job openings for freshers and also work from home job openings. This is the best website to apply for off campus drive in India. Visit our website for government job alerts and private job alerts. We also list free interview notes and study materials, one of the best interview study website.comfortable to face the interviews:

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

#### Approach 1: Recursive

• Time: O(n)O(n)
• Space: O(n)O(n)

### Approach 1: REVERSE NODES IN K-GROUP Program Solution in C++

``````class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
return nullptr;

for (int i = 0; i < k; ++i) {
if (!tail)  // less than k nodes, do nothing
tail = tail->next;
}

}

private:
ListNode* reverse(ListNode* head, ListNode* tail) {
ListNode* prev = nullptr;

while (curr != tail) {
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}

return prev;
}
};
``````

### Approach 1: REVERSE NODES IN K-GROUP Program Solution in JAVA

``````class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
return nullptr;

for (int i = 0; i < k; ++i) {
if (!tail)  // less than k nodes, do nothing
tail = tail->next;
}

}

private:
ListNode* reverse(ListNode* head, ListNode* tail) {
ListNode* prev = nullptr;

while (curr != tail) {
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}

return prev;
}
};
``````

### Approach 1: REVERSE NODES IN K-GROUP Program Solution in Python

``````class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
return nullptr;

for (int i = 0; i < k; ++i) {
if (!tail)  // less than k nodes, do nothing
tail = tail->next;
}

}

private:
ListNode* reverse(ListNode* head, ListNode* tail) {
ListNode* prev = nullptr;

while (curr != tail) {
ListNode* next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}

return prev;
}
};
``````

Approach 2: Iterative

• Time: O(n)O(n)
• Space: O(1)O(1)

### Approach 2: REVERSE NODES IN K-GROUP Program Solution in C++

``````class Solution {
public:
ListNode* reverseKGroup(ListNode* head, int k) {
if (!head || k == 1)

ListNode* prev = &dummy;

for (int i = 0; i < length / k; ++i) {
for (int j = 0; j < k - 1; ++j) {
ListNode* next = curr->next;
curr->next = next->next;
next->next = prev->next;
prev->next = next;
}
prev = curr;
curr = curr->next;
}

return dummy.next;
}

private:
int length = 0;
for (ListNode* curr = head; curr; curr = curr->next)
++length;
return length;
}
};
``````

### Approach 2: REVERSE NODES IN K-GROUP Program Solution in JAVA

``````class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
if (head == null || k == 1)

ListNode dummy = new ListNode(0, head);
ListNode prev = dummy;

for (int i = 0; i < length / k; ++i) {
for (int j = 0; j < k - 1; ++j) {
ListNode next = curr.next;
curr.next = next.next;
next.next = prev.next;
prev.next = next;
}
prev = curr;
curr = curr.next;
}

return dummy.next;
}

int length = 0;
for (ListNode curr = head; curr != null; curr = curr.next)
++length;
return length;
}
}
``````

### Approach 2: REVERSE NODES IN K-GROUP Program Solution in Python

``````class Solution:
def reverseKGroup(self, head: ListNode, k: int) -> ListNode:
if not head or k == 1:

length = 0
length += 1
return length

prev = dummy