Reverse Alternate K Nodes in a Singly Linked List

Given a singly linked list and an integer 'k' reverse every 'k' element.

Statement

Given a singly linked list and an integer k, write a function to reverse every k element.

  • If k<=1k <= 1, then the input list is unchanged.
  • If k>=nk >= n (nn is the length of the linked list), then reverse the linked list according to k.

Example

Below is an example of an input and output linked list after reversing every 33 elements:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.