#3595. [GESP202606 五级 C++] 第 2 题

[GESP202606 五级 C++] 第 2 题

下面代码遍历并输出一个循环单链表,其中 head 指向链表的第一个节点,横线处应填入的是( )。

struct Node {
    int val;
    Node* next;
};
void printList(Node* head) {
    if (head == nullptr) return;
    Node* p = head;
    _______________________ // 在此处填入代码
    cout << endl;
}

A.

while (p != nullptr) {
    cout << p->val << " ";
    p = p->next;
}

B.

while (p->next != nullptr) {
    cout << p->val << " ";
    p = p->next;
}

C.

do {
    cout << p->val << " ";
    p = p->next;
} while (p != head);

D.

for (; p; p = p->next) {
    cout << p->val << " ";
}

{{ select(1) }}

  • A
  • B
  • C
  • D