Test instructions: Returns the linked list of the specified segment [M,n], returning the chain header.
Idea: The main trouble in the chain table head, if you want to start from the linked list, more special.
At present, with the implementation of DFS, first find the location of the m-1, and then find the location of n+1, the middle of this paragraph on whether to reverse, to the DFS to solve, with a counter to count the number of the reverse has been.
1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7 * };8 */9 classSolution {Ten Public: Onelistnode* DFS (listnode* t,listnode* far,intnum) A { - if(num==0)returnFar ; -listnode* tmp=t->Next; thet->next=Far ; - returnDFS (tmp, T, num-1); - } - +listnode* Reversebetween (listnode* head,intMintN) { - if(m==n)returnhead; +listnode* T=head, *r=head; A at for(intI=0; i<n; i++) r=r->next;//find the first n+1 - if(m==1)returnDFS (Head, R, n-m+1); - - for(intI=1; i<m-1; i++) t=t->next;//find the first m-1 -T->next=dfs (T->next, R, n-m+1); - returnhead; in } -};
AC Code
Leetcode Reverse Linked list II reverse linked List 2