Write an algorithm to reverse a single linked list.
Anonymous
class ListNode { public: int val; ListNode* next; ListNode(int x) { val=x; next=NULL; }; }; class Solution { public : ListNode* ReverseList(ListNode* A) { stack s; ListNode* temp=A,*ans; while (temp!=NULL) { s.push(temp); temp=temp->next; } temp=s.top(); s.pop(); ans=temp; while(!s.empty()) { temp->next=s.top(); temp=s.top(); s.pop(); } temp->next=NULL; return ans; } };
Check out your Company Bowl for anonymous work chats.