Reverse linked list ② (Force Link No.92)

Code:

ListNode* reverseBetween(ListNode* head, int left, int right) { int change_len = right - left + 1; ListNode* pre_head = nullptr; ListNode* result = head; ListNode* result = head; // while (head && --left) {// if left=1, nullptr pre_head = head; // if left=1, nullptr pre_head = head; Head = head->next; } ListNode* modify_list_tail = head; ListNode* new_head = nullptr; // Save the first node of the segment modify_list_tail. While (head && change_len) {ListNode* next = head->next; head->next = new_head; new_head = head; head = next; change_len--; } modify_list_tail->next = head; If (pre_head) {pre_head->next = new_head; } else { result = new_head; } return result; }Copy the code