educative.io

What is making while loop repeat itself after the conditon is not true anymore?

what is making this loop repeat itself after the first iteration? once we reverse the first list, we need to keep reversing the next set of k nodes with this loop but I’m confused about how it knows to run again instead of going to the next part of the solution?

while (current !== null && i < k) { // reverse 'k' nodes
          console.log('run')
          next = current.next;
          current.next = previous;
          previous = current;
          current = next;
          i += 1;
        }

Course: Grokking the Coding Interview: Patterns for Coding Questions - Learn Interactively
Lesson: Reverse every K-element Sub-list (medium) - Grokking the Coding Interview: Patterns for Coding Questions

Hi Jennifer Herrate. Let me try to answer your query. This loop reverses k nodes in a list and then stops working. There is an outer loop While(True) which calls this loop again to reverse the next k nodes until we reach the end of the list. Hope this answers your query.