Reverse Linked List II
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.
https://leetcode.com/problems/reverse-linked-list-ii/
Flips the linked list from M to N.
The flip operation is the same as the previous question: http://www.cnblogs.com/Liok3187/p/4540490.html
1 /**2 * Definition for singly-linked list.3 * Function ListNode (val) {4 * This.val = val;5 * this.next = null;6 * }7 */8 /**9 * @param {listnode} headTen * @param {number} m One * @param {number} n A * @return {ListNode} - */ - varReversebetween =function(Head, M, N) { the varres =NewListNode ( -1), subhead = res, count = 1; -Res.next =head; - while(Count!==m) { -Subhead =head; +Head =Head.next; -count++; + } A varSubtail =head, TMP; at while(Count!== n + 1){ -TMP =Head.next; -Head.next =Subhead.next; -Subhead.next =head; -Head =tmp; -count++; in } -Subtail.next =head; to returnRes.next; +};
[Leetcode] [JavaScript] Reverse Linked List II