educative.io

Unclear example

The example is not very clear. What happens if there are more than 2 even numbered nodes, e.g. if there are 6 nodes. Would 2 swap with 4 and 6 be unchanged or would 2 swap with 4 and 4 would swap with 6?

1 Like

Hi Hasan,

Thank you for reaching out to us and giving your feedback. Let’s take an example here:

[1,2,3,4,5,6] after reversing this it will be like that [1,6,3,4,5,2] as this will 2 and insert at the head of the linked list and then 4 at the head and then 6 at the head, so after linking again with the original linked list, it will be displayed like this [1,6,3,4,5,2].

If you have any further concerns/questions/comments, please let us know.

Best regards,
Educative Team

I had the same question. From the description, it seems that you are swapping even-numbered pairs, and proceeding to the next pair. Reversing all the even nodes is much easier.

I think using the language “reverse all the even nodes in the list, leaving odd nodes in their original position” would save others from trying to implement what is a much harder algorithm.