Reverse Linked List II Posted on 2018-10-05 Descriptionhttps://leetcode.com/problems/reverse-linked-list-ii/description/ Solution123456789101112131415161718192021222324252627282930313233343536373839404142/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode* reverse(ListNode* head) { if (!head || !head->next) return head; ListNode* ret = reverse(head->next); head->next->next = head; head->next = NULL; return ret; } ListNode* reverseBetween(ListNode* head, int m, int n) { if (head == NULL || head->next == NULL || m == n) return head; ListNode* fake = new ListNode(-1); fake->next = head; ListNode* start = fake; while (m > 1) { start = start->next; --m; --n; } ListNode* end = start; while (n > 0) { --n; end = end->next; } ListNode* temp = end; end = end->next; temp->next = NULL; ListNode* h = reverse(start->next); start->next->next = end; start->next = h; return fake->next; }};