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.
it is explained in very deep detail related to C++ solution above.
As logic is the same it should be possible for you to understand
above statements, but may be not explicitly related to your question only.
So,
head is reference to "head" before the next call of a recursion. head.next is referencing to same "head" as within next recursion call.
And it's next is (head.next).next, which is the pointer to next element. This pointer is set to "head", which is basically previous element. This is exactly what we want. Instead of pointing to next element we point to previous element, causing reversing the list.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Reverse a linked list
You are viewing a single comment's thread. Return to all comments →
it is explained in very deep detail related to C++ solution above. As logic is the same it should be possible for you to understand above statements, but may be not explicitly related to your question only. So, head is reference to "head" before the next call of a recursion. head.next is referencing to same "head" as within next recursion call. And it's next is (head.next).next, which is the pointer to next element. This pointer is set to "head", which is basically previous element. This is exactly what we want. Instead of pointing to next element we point to previous element, causing reversing the list.