Reverse a linked list from position m to N. Do it in-place and in One-pass.
For example:
Given 1->2->3->4->5->NULL
, m = 2 and n = 4,
Return 1->4->3->2->5->NULL
.
Note:
Given m, n satisfy the following condition:
1 ≤ m ≤ n ≤length of list.
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* Reversebetween (listnode* head,intMintN) { A intd=n-m; - if(head==null| | head->next==null| | d<=0) - returnhead; the -listnode* p=head,*bfp=p; -listnode* q=head; - for(intI=0; i<m-1; i++){ +bfp=p; -P=p->Next; + } Alistnode* revhead=p; atlistnode* bf=p,*bh=NULL; -P=p->Next; -d--; - while(p->next!=null&&d!=0){ -d--; -Bh=p->Next; inp->next=BF; -bf=p; top=BH; + - } theBh=p->Next; *p->next=BF; $bfp->next=p;Panax Notoginsengrevhead->next=BH; - if(m!=1) the returnhead; + Else returnp; A } the};
"Leetcode" 92. Reverse Linked List II