``` private static ListNode ReverseKList(ListNode head, int k) { var setGroup = true; ListNode firstNode = null; ListNode secondTail = null; ListNode firstTail = null; ListNode prev = null; if (head == null || head.Next == null || k == 1) { return head; } int length = 0; //find length of list ListNode temp = head; while (temp != null) { temp = temp.Next; length++; } if (k > length) { return head; } //reverse the list in k group var group = k; while (head != null) { //reverse the list var next = head.Next; head.Next = prev; prev = head; head = next; if (setGroup) { // get the first group tail node address // pre will have tail node intilally for each iteration of a group if (firstTail == null) firstTail = prev; secondTail = prev; setGroup = false; } if (--group == 0 || head == null) { if(firstNode==null) firstNode = prev; else { //link the two reversed grouped firstTail.Next = prev; firstTail = secondTail; } prev = null; group=k; setGroup = true; } } return firstNode; }