Topic:
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.
Ideas:
Find the M node, reverse the pointer from node m to N, and then concatenate the flip-up
/** Definition for singly-linked list. * Function ListNode (val) {* This.val = val; * This.next = null; *} /c13>*//** * @param {listnode} head * @param {number} m * @param {number} n * @return {ListNode}*/varReversebetween =function(Head, M, N) {if(head==NULL){ returnHead; } varP=head,mpre=NULL; varTemphead=NewListNode (0); Temphead.next=Head; Mpre=Temphead; for(vari=1;i<m;i++) {Mpre=p; P=P.next; } varPre=NULL, cur=NULL, next=NULL; vartempp=p; for(vari = 1; I <= n-m; i++) {//Invert M-to-N pointersPre =p; Cur=P.next; Next=Cur.next; Cur.next=Pre; P=cur; } Mpre.next=p; Tempp.next=Next; Head=Temphead.next; Temphead=NULL; returnhead;};
"Linked list" Reverse Linked list II