Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

80. Reverse Nodes in k-Group

HardDay 24 of 60Linked List

Given the head of a linked list and a positive integer k, reverse the nodes in groups of k and return the new head. If the number of nodes left at the end is less than k, keep that last part in its original order. Change the links between nodes; do not just swap values.

Example 1
Input: head = [1,2,3,4,5], k = 2
Output: [2,1,4,3,5]
Groups [1,2] and [3,4] are reversed; [5] is shorter than k and stays.
Example 2
Input: head = [1,2,3,4,5], k = 3
Output: [3,2,1,4,5]
Constraints
  • 1 <= k <= n <= 5000
  • 0 <= Node.val <= 1000
Hints
Frequently asked at: Microsoft, Amazon, Meta, Adobe
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min