Let's take an easy example of 8 -> 4
again. let rest = reverseList(head.next);
takes 4
and calls reverseList
on it.
Calling reverseList
on 4
will have us reach the termination clause because there is no .next
:
1if (!head || !head.next) {
2 return head;
3}