CodeRevisited Keep Learning.. Cheers!

Reverse every k nodes of a linked list

Problem

Write a function to reverse every k nodes of a linked list (k is the input to the function)

Example:

Given linked list is 1->2->3->4->5->6->7->8>9, Input: K = 3
Output: 3->2->1->6->5->4->9->8->7
Input: K = 5
Output: 5->4->3->2->1->9->8->7->6

Iterative Solution

Recursive Solution

Check here for complete code.