Reverse Linked List(With Data)

PHOTO EMBED

Sun May 26 2024 12:58:05 GMT+0000 (Coordinated Universal Time)

Saved by @ayushg103 #c++

ListNode* reverseList(ListNode* head) {
        stack<int> s;
        ListNode* t=head;
           while(t!=NULL)
           {
             s.push(t->val);
             t=t->next;
           }
           t=head;
             while(t!=NULL)
           {
             t->val = s.top();
             s.pop();
             t=t->next;
           }
           return head;
    
content_copyCOPY

put the elements of LL in stack; then re enter the elements in LL(Reversing with the help of data) make sure u update t to head(t=head) after the first loop T = O(N) s = O(N)