# Reverse Alternate K nodes

Posted: 22 Nov, 2020

Difficulty: Easy

#### You are given a Singly Linked List of integers and a positive integer 'K'. Modify the linked list by reversing every alternate 'K' nodes of the linked list.

```
A singly linked list is a type of linked list that is unidirectional, that is, it can be traversed in only one direction from head to the last node (tail).
```

##### 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: 5 6 7 8 9 10 11 12
K: 3
Output: 7 6 5 8 9 10 12 11
We reverse the first 'K' (3) nodes and then skip the next '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).
```

##### Note:

```
You need to reverse the first 'K' nodes and then skip the 'K' nodes and so on. 5 6 7 10 9 8 11 12 is not the correct answer for the given linked list.
```

##### 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, return 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 <= N
-10^3 <= data <= 10^3 and data != -1
Time Limit: 1 sec
```

Approach 1

The idea is very simple. We will process 2 * ‘K’ nodes at a time. Firstly, we will reverse the first ‘K’ nodes of the linked list and then we will skip the next ‘K’ nodes. We will do this recursively for the remaining linked list.

**Algorithm:**

- If the node does not exist, simply return ‘NULL’.
- Head is pointing to the first node of the linked list.
- If there are less than ‘K’ nodes, just reverse them and return the reversed linked list. Else reverse the first ‘K’ nodes of the linked list.
- After reversing the ‘K’ nodes, the head points to the ‘K’th node and the ‘K’th node in the original linked list will become the new head.
- To connect the reversed part with the remaining part of the linked list, update the next of the head to (‘K’ + 1)th node.
- As we don’t have to reverse the next ‘K’ nodes, we will skip them.
- Now, recursively modify the rest of the linked list and link the two sub-linked lists.
- Return the new head of the linked list.

Approach 2

The idea is the same as used in the previous approach. This time, we will do it iteratively.

- Head is pointing to the first node of the linked list.
- Initialise a pointer to a Node ‘NEW_HEAD' that will point to the head of the final modified linked list.
- Repeat the steps below until all the nodes are processed in the same way.

- Reverse the first ‘K’ nodes of the linked list.
- After reversing the ‘K’ nodes, the head points to the ‘K’th node.
- If the 'NEW_HEAD' is ‘NULL’:
- If the Kth node in the original linked list exists, it will be the 'NEW_HEAD'.
- Else, the last node in the original linked list will be the 'NEW_HEAD'.

- To connect the reversed part with the remaining part of the linked list, update the next of the head to (‘K’ + 1)th node.
- As we don’t have to reverse the next ‘K’ nodes, we will skip them and move to the next iteration.

4. Return the new head of the linked list.

Note that we just have to set the new head once.

SIMILAR PROBLEMS

# Binary Linked List To Integer

Posted: 22 Sep, 2021

Difficulty: Easy

# BST from Preorder

Posted: 1 Nov, 2021

Difficulty: Moderate

# Intersection of Linked Lists

Posted: 12 Dec, 2021

Difficulty: Easy

# Ninja’s Training

Posted: 16 Dec, 2021

Difficulty: Moderate

# Frog Jump

Posted: 17 Dec, 2021

Difficulty: Easy