题目:
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表
class Solution {
public:
ListNode* reverseList(ListNode* head) {
return reverse(nullptr,head);
}
ListNode* reverse(ListNode* pre,ListNode*cur)
{
if(cur == nullptr) return pre;
ListNode*tmp = cur->next;
cur->next = pre;
return reverse(cur,tmp);
}
};
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode*pre = nullptr;
ListNode*cur = head;
ListNode*tmp ;
while(cur)
{
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
};