1
var reverseEvenLengthGroups = function (head) {11
while (curr != null) {12
if (count === groupSize) {13
if (groupSize % 2 === 0) {14
// we only reverse when it is even16
const tail = start.next; // the starting node of the reverse linked list will be the tail after the reverse takes place17
reverseList(start, end, count); // we need to reverse everything in the middle of start and end18
start = tail; // we set the new start to the end of the reversed linked list20
// when groupSize is even we don't need to reverse, but need to set the new start to the prev node23
count = 0; // whenever we reached the group size we need to reset our count and up our groupSize26
// just a normal traversal when we haven't hit our groupSize33
if (count % 2 === 0) {34
// in the case where we ended early on even count35
reverseList(start, null, count);40
function reverseList(start, end, count) {41
if (start.next == null) return start; // for case when we have a single node45
let curr = start.next;46
let tail = start.next;48
for (let i = 0; i < count; ++i) {49
const next = curr.next;