Insert a node at a specific position in a linked list

  • + 1 comment

    Just to help clear things up in case anyone is confused like I was. Like others have hinted at, the initial '2' value is important. Even though it shouldn't be necessary for the purposes of this test, for the test to pass, the first '2' should ideally stay in your linked list and your algorithm would just keep that first '2' at the tail as it inserts new values. The code seems to expect the '2' to be at the end so I guess the test omits the last item of your linked list when printing and comparing the results at the end of each test.

    I tried to be clever and remove the 2 at the start and this didn't work, as my algorithm ended up producing this for Test Case 0:

    3 -> 10 -> 5 -> 4 -> 2

    The test will print it out as: 31054 (notice the last value is missing)

    Therefore you should have your algorithm produce this (for Test Case 0):

    3 -> 10 -> 5 -> 4 -> 2 -> 2

    And the test will print it out as: 310542