educative.io

Rotate a LinkedList (medium)# Using fast and slow pointers

function rotate(head, rotations) {
  let fast = head, slow = head, i = 0;
  while (i < rotations && fast) {
    fast = fast.next;
    i++;
  }

  // move both fast and slow till we reach the end.
  while (fast.next) {
    slow = slow.next;
    fast = fast.next;
  }

  fast.next = head;
  head = slow.next;
  slow.next = null;

  return head;
};

Hi @Nisha_Karamchandani
The solution provided in the course covers two cases.
Case 1
when the rotation is larger or equal to the length of the linked list.
rotation >= length of list
Case 2
when the rotation is smaller than the length of the linked list.
rotation < length of list
On the other hand, your code will crash when it tries to solve Case 1 because after the first loop, the variable fast = null. As a result, the following commands cannot be executed.
I hope this will help