下⾯的代码⽚段⽤于反转单链表 ,请进⾏( ) 修改 ,使其能正确实现相应功能。
1 ListNode* reverseLinked List(ListNode* head) { 2 ListNode* prev = nullptr; 3 ListNode* current = head; 4 while (current != nullptr) { 5 ListNode* next = current->next; 6 current->next = next; 7 prev = current; 8 current = next; 9 } 10 return prev; 11 }
current->next = next; 应该改为 current->next = prev;
ListNode* next = current->next; 应该改为 ListNode* next = prev->next;
current != nullptr 应该改为 current->next != nullptr
ListNode* prev = nullptr; 应该改为 ListNode* prev = head;