Writing a LinkedList destructor?

后端 未结 6 1873
我在风中等你
我在风中等你 2020-12-30 05:36

Is this a valid LinkedList destructor? I\'m still sort of confused by them.

I want to make sure I\'m understanding this correctly.

 LinkedList::~Link         


        
6条回答
  •  执念已碎
    2020-12-30 06:10

    Why not do it much much simpler - with an elegant while-loop instead of trying to carefully analyze whether that overcompilcated for-loop is correct?

    ListNode* current = head;
    while( current != 0 ) {
        ListNode* next = current->next;
        delete current;
        current = next;
    }
    head = 0;
    

提交回复
热议问题