{"size": 1057, "ext": "js", "max_stars_repo_path": "linkedList/92.reverseLinkedList2.js", "max_stars_repo_name": "ZhengAu/leetCode", "max_stars_repo_stars_event_min_datetime": "2021-08-04T15:54:35.000Z", "max_stars_repo_stars_event_max_datetime": "2021-08-04T15:54:35.000Z", "max_issues_repo_path": "linkedList/92.reverseLinkedList2.js", "max_issues_repo_name": "ZhengAu/leetCode", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "linkedList/92.reverseLinkedList2.js", "max_forks_repo_name": "ZhengAu/leetCode", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 19.5740740741, "max_line_length": 66, "alphanum_fraction": 0.6017029328}
/**
 * Reverse a linked list from position m to n. Do it in one-pass.
 * Note: 1 ≤ m ≤ n ≤ length of list.
 * @author zheng
 * @date 2020/10/14 11:29:45
 */

/**
 * 移动结点，注意结点要先连接后断开
 * 注意插入的位置，以及连接的位置
 */
const { ListNode, createList, traversal } = require('./ListNode');

/**
 * Time complexity: O(N)
 * Space complexity: O(1)
 * @param {ListNode} head
 * @param {number} m
 * @param {number} n
 * @return {ListNode}
 */
const reverseBetween = function (head, m, n) {
	if (!head) return head;
	let dummy = new ListNode(0, head);

	// 找到m前的结点
	let start = 1;
	if (m !== 1) {
		while (head) {
			start++;
			if (start === m) break;
			head = head.next;
		}
	} else {
		head = dummy;
	}

	let count = n - m; //需移动多少个结点
	let pp = head.next; //第m个结点不需要移动，从m+1个开始移动
	while (pp && count > 0) {
		let newHead = head.next;
		head.next = pp.next;
		pp.next = pp.next.next;
		head.next.next = newHead;
		count--;
	}
	return dummy.next;
};

let arr = [1, 2, 3, 4, 5];
let head = createList(arr);
let res = reverseBetween(head.next, 1, 5);
console.log(traversal(res));
