Easy Understanding - using stack


  • 0
    L
    class Solution {
    public:
        ListNode* reverseList(ListNode* head) {
            if(head==NULL)
                return NULL;
            stack<ListNode*> st;
            while(head->next!=NULL){
                st.push(head);
                head = head->next;
            }
            ListNode* newSt = head; // new head
            while(!st.empty()){
                ListNode* nextN = st.top();
                st.pop();
                head->next = nextN;
                head = head->next;
            }
            head->next = NULL;
            
            
            return newSt;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.