力扣206 反转链表

104 阅读1分钟
/**
 * 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==nullptr||head->next==nullptr)  return head;
        ListNode*p=reverseList(head->next);
        ListNode*tail=head->next;
        head->next=tail->next;
        tail->next=head;
        return p;
         }
};

这题就是反转链表系列问题的祖宗 递归无脑过