单选题

为了方便链表的增删操作,一些算法生成一个虚拟头节点,方便统一删除头节点和其他节点。下面代码实现了删除链表中值为 val 的节点,横线上应填的最佳代码是(    )。

struct LinkedNode {

int val;

LinkedNode* next;

LinkedNode(int val):val(val), next(nullptr) {

}

};

void removeElements(LinkedNode* head, int val) {

if (head == nullptr) {

    return;

}

LinkedNode* cur;

LinkedNode* dummyHead = new LinkedNode(0); //虚拟头节点

________________________________ // 在此处填入代码

while(cur ->next != nullptr) {

if(cur->next->val == val) {

LinkedNode* tmp = cur->next;

cur->next = cur->next->next;

delete tmp;

tmp = nullptr;

} else {

cur = cur ->next;

}

}

head = dummyHead->next;

delete dummyHead;

dummyHead = nullptr;

}

A

dummyHead->next = head; cur = dummyHead;

B

dummyHead->next = head->next; cur = dummyHead;

C

dummyHead->next = head; cur = dummyHead->next;

D

dummyHead->next = head->next; cur = dummyHead->next;

赣ICP备20007335号-2