假设链表是单向循环链表(尾节点指回头节点),下面遍历打印代码横线处应填入( )。
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 << " "; }
正确答案:C