We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Insert a node at a specific position in a linked list
You are viewing a single comment's thread. Return to all comments →
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