Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6376 |

@mb1973 | 5604 |

@Quandray | 5231 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4510 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3413 |

Complete Leaderboard | |

Handle | Score |

@codersgroup18 | 799 |

@sriramgoparaju99 | 737 |

@Manikanta punnam | 629 |

@rdakka | 606 |

@prankursharma31 | 597 |

@praveenbgp6 | 546 |

@sanskar94511 | 531 |

@purohitmn02 | 529 |

@yashkaril4 | 517 |

@abhikarshgupta | 474 |

@rohitgarg2825 | 464 |

Complete Leaderboard |

Given a linked list of size **N**. The task is to reverse every **k** nodes (where k is an input to the function) in the linked list.

**Example 1:**

**Input:
**LinkedList: 1->2->2->4->5->6->7->8
K = 4
**Output: **4 2 2 1 8 7 6 5
**Explanation:**
The first 4 elements 1,2,2,4 are reversed first
and then the next 4 elements 5,6,7,8. Hence, the
resultant linked list is 4->2->2->1->8->7->6->5.

**Example 2:**

**Input:
**LinkedList: 1->2->3->4->5
K = 3
**Output: **3 2 1 5 4
**Explanation: **
The first 3 elements are 1,2,3 are reversed
first and then elements 4,5 are reversed.Hence,
the resultant linked list is 3->2->1->5->4.

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **reverse**() which should reverse the linked list in group of size **k **and return the head of the modified linked list.

**Expected Time Complexity **: O(N)

**Expected Auxilliary Space **: O(1)

1 <= N <= 10^{4}

1 <= k <= N

1 <= k <= N

Show more..

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Reverse a Linked List in groups of given size.

...