写一个链表MakeEmpty()
时间: 2024-03-30 16:21:24 浏览: 146
数据结构——指针和数组.md
Sure, here is the implementation for the MakeEmpty() function for a singly linked list in C++:
void MakeEmpty(ListNode* head) {
ListNode* current = head->next;
ListNode* next;
head->next = nullptr;
while (current != nullptr) {
next = current->next;
delete current;
current = next;
}
}
This function deallocates all the nodes in the linked list and sets the head pointer to nullptr to indicate an empty list.
阅读全文