# Reverse DLL nodes in groups

Posted: 2 Dec, 2020

Difficulty: Moderate

#### You are given a Doubly Linked List of integers and a positive integer 'K' representing the group size. Modify the linked list by reversing every group of 'K' nodes in the linked list.

```
A doubly linked list is a type of linked list that is bidirectional, that is, it can be traversed in both directions, forward and backward.
```

###### Note:

```
If the number of nodes in the list or in the last group is less than K, just reverse the remaining nodes.
```

##### Example:

```
Linked list: 8 9 10 11 12
K: 3
Output: 10 9 8 12 11
We reverse the first K (3) nodes. Now, since the number of nodes remaining in the list (2) is less than K, we just reverse the remaining nodes (11 and 12).
```

##### Input Format

```
The first line of input contains an integer T, the number of test cases.
The first line of every test case contains the elements of the singly linked list separated by a single space and terminated by -1. Hence, -1 would never be a list element.
The second line of every test case contains the positive integer ‘K’.
```

##### Output Format:

```
For every test case, print the modified linked list. The elements of the modified list should be single-space separated, terminated by -1.
```

##### Note:

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

##### Constraints:

```
1 <= T <= 10
1 <= N <= 5 * 10^4
1 <= K <= 10^5
-10^3 <= data <= 10^3 and data != -1
Time Limit: 1 sec
```

SIMILAR PROBLEMS

Insertion In A Singly Linked List

Posted: 22 Apr, 2022

Difficulty: Easy

Remove Loop

Posted: 22 Apr, 2022

Difficulty: Moderate

Deletion In Doubly Linked List

Posted: 22 Apr, 2022

Difficulty: Easy

Insertion In Doubly Linked List

Posted: 24 Apr, 2022

Difficulty: Easy

LRU Cache

Posted: 10 Sep, 2022

Difficulty: Moderate