
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (head == nullptr || head->next == nullptr)
return head;
ListNode * p = reverseList(head->next);
head->next->next = head;
head->next = nullptr;
return p;
}
};
//作者:LeetCode
//链接:https://leetcode-cn.com/problems/reverse-linked-list/solution/fan-zhuan-lian-biao-by-leetcode/
//来源:力扣(LeetCode)
//著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
本文介绍了一种使用递归方法来反转单链表的算法。通过定义链表节点结构和递归函数,实现了链表的反转操作。此方法在LeetCode上得到了验证,提供了完整的代码实现。
521

被折叠的 条评论
为什么被折叠?



