0. 题目
1. 递归
class Solution {
public:
ListNode* reverseN(ListNode* head,int n) { //反转n个节点 n=2 :[1,2,3,4,5]反转1,2
if(n==1) return head;
ListNode *tail = head->next; //2 3 4 5(tail指向反转链表 反转部分的尾节点)
ListNode *p=reverseN(head->next,n-1); // p=(2,3,4,5),n=1
head->next = tail->next; //头插法,插入head:1 , 得到 (2 1)3 4 5
tail->next = head;
return p;
}
ListNode* reverseBetween(ListNode* head, int l, int r) {
//虚拟头节点,用于返回
ListNode ret(0,head);
ListNode *p=&r