• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
September 02, 2023 |590 Views
SDE Sheet - Reverse a Linked List in group of given size
  Share   Like
Description
Discussion

This video is part of the Linked List section in the GFG SDE Sheet.

In this video, we are given a linked list of size N. Our task is to reverse every k nodes (where k is an input to the function) in the linked list. If the number of nodes is not a multiple of k then left-out nodes, in the end, should be considered as a group and must be reversed.

Example :

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.

Do check out:-

Article: https://www.geeksforgeeks.org/reverse-a-linked-list-in-groups-of-given-size/
Problem: https://practice.geeksforgeeks.org/problems/reverse-a-linked-list-in-groups-of-given-size/1
SDE Sheet: https://www.geeksforgeeks.org/sde-sheet-a-complete-guide-for-sde-preparation/

Read More