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.
// Non-recursive version, using stack#include<stack>Node*Reverse(Node*head){if(head!=nullptr){Node*curr=head;std::stack<Node*>ps;while(curr){ps.push(curr);curr=curr->next;}Node*newhead=ps.top();while(!ps.empty()){Node*prev=ps.top();ps.pop();prev->next=ps.empty()?nullptr:ps.top();}returnnewhead;}returnhead;}
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 →
Three solutions listed here: