Ad
  • Custom User Avatar

    Yes and no.

    • You're right about readability.
    • Mine has actually a slightly better genericity/logical setup.

    So in the end, the actual best practice would be to combine both approaches.

  • Custom User Avatar

    It doesn't recreate the whole list. It recurses through the list until it finds the correct index, then inserts the new Node at that location. If you're referring to head.next = insert_nth(head.next, index - 1, data), that doesn't "recreate" anything, it just sets the new assignment which, in most cases, is the same as the old assignment.

    If the linked list contains that many elements, then this function will fail. However, lists of that size were not part of this kata. That being said, if I wrote this again, I'd make it tail recursive, and then the list size would not be limited by the stack size.

  • Custom User Avatar

    nice solution (your solution)