leetcode 206—>反转链表(递归实现)
有不懂的欢迎在评论区提问:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if (head == NULL || head->next == NULL) return head;
ListNode* new_head,*tail;
tail = head->next;
new_head = reverseList(head->next);
head->next = tail->next;
tail->next = head;
return new_head;
}
};