Reverse Linked List in Groups of Size K
Hard Problems of Linked List DSA practice problem on Onlearn.
Difficulty: hard.
Topics: Reverse nodes of a singly linked list in groups of K, Linked List, Singly Linked List, Node, Pointer, Algorithm, Time Complexity, Space Complexity, In-place Algorithm, linked list traversal, pointer operations, linked list, linked list manipulation, time complexity analysis, Linked List Reversal, Data Structure Operations.
Given the head of a singly linked list of n nodes and an integer k, where k is less than or equal to n. Your task is to reverse the order of each group of k consecutive nodes. If n is not divisible by k, then the last group of remaining nodes should remain unchanged. Input Format: The first line contains the elements of the linked list. The second line contains an integer k. Output Format: The modified linked list after reversing nodes in groups of k. Example 1: Input: LL: 1 2 3 4 5 6 7 8 9 10 K = 3 Output: 3 2 1 6 5 4 9 8 7 10 Explanation: Group 1: Reverse nodes 1 2 3 to become 3 2 1. Group 2: Reverse nodes 4 5 6 to become 6 5 4. Group 3: Reverse nodes 7 8 9 to become 9 8 7. Node 10 remains as is since there are no more groups of size K remaining. Example 2: Input: LL: 5 4 3 7 9 2 K = 4 Output: 7 3 4 5 9 2 Explanation: Group 1: Reversed nodes 5 4 3 7 to become 7 3 4 5. Group 2: Nodes 9 2 remain unchanged as they are not a complete group of size K.