给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
核心:要反转,反之前未预防断点要保存该点
Listbode* temp = cur->next;
cur->next = pre;
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode * pre = nullptr;
ListNode *curr = head;
while(curr != nullptr){
ListNode * temp = curr->next;
curr->next = pre;
pre = curr;
curr = temp;
}
return pre;
}
};