fun reverseNonEmptyListRecursive(node: ListNode): ListNode { val nextNode = node.next return if (nextNode == null) node else { // The recursion stack frame will be pushed until it reached the tail node val reversed = reverseNonEmptyListRecursive(nextNode) // then it will start popping // reversing process nextNode.next = node // this line is to set the reversed list's tail(originally head)'s `next` to `null` // for all nodes other than original head, // `node.next = null` would be overwritten by the statement // `nextNode.next = node` in the one-level shallower recursion stack frame node.next = null reversed } } return reverseNonEmptyListRecursive(head)