迭代:
开一个新链表,头插法
ListNode* reverse(ListNode* root){
ListNode* head=nullptr,cur=root,nxt;
while(cur!=nullptr){
nxt=cur->next;
cur->next=head;
head=cur;
cur=nxt;
}
return head;
}
递归
ListNode* reverse(ListNode* root){
if(!root->next) return root;
ListNode* head = reverse(root->next);
root->next->next = root;
root->next = nullptr;
return head;
}