面试题答案
一键面试#include <iostream>
// 定义链表节点结构
struct ListNode {
int data;
ListNode* next;
ListNode(int val) : data(val), next(nullptr) {}
};
// 函数实现将链表中所有节点的数据域值翻倍
void doubleValues(ListNode* head) {
ListNode* current = head;
while (current != nullptr) {
current->data *= 2;
current = current->next;
}
}
// 辅助函数:打印链表
void printList(ListNode* head) {
ListNode* current = head;
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
// 辅助函数:释放链表内存
void freeList(ListNode* head) {
ListNode* current = head;
ListNode* nextNode;
while (current != nullptr) {
nextNode = current->next;
delete current;
current = nextNode;
}
}
你可以这样使用上述代码:
int main() {
// 创建链表 1 -> 2 -> 3
ListNode* head = new ListNode(1);
head->next = new ListNode(2);
head->next->next = new ListNode(3);
std::cout << "Original list: ";
printList(head);
doubleValues(head);
std::cout << "Doubled list: ";
printList(head);
freeList(head);
return 0;
}
函数内部指针操作的关键步骤:
- 初始化指针:定义一个指针
current
并使其指向链表头节点head
。 - 遍历链表:通过
while
循环,在current
不为nullptr
的情况下,不断移动current
到下一个节点。 - 数据翻倍:在每次循环中,将
current
所指向节点的数据域data
乘以 2。 - 移动指针:通过
current = current->next
将current
移动到下一个节点,以便处理下一个节点的数据。
可能出现的问题及解决方案:
- 空链表:如果传入的
head
是nullptr
,函数应能正确处理,在上述实现中,while
循环条件可以处理这种情况,因为current
初始化为head
,如果head
为nullptr
,循环不会执行。 - 内存泄漏:在处理链表时,如果在修改数据过程中发生异常,可能会导致部分节点内存未释放。为避免这种情况,在使用完链表后,需要编写释放链表内存的函数(如
freeList
函数),在程序结束前调用以确保所有节点内存被正确释放。