[LeetCode][JavaScript]Reverse Linked List II

本文介绍了如何在链表中翻转从位置m到n的区间,并提供了代码实现,包括定义链表节点、翻转链表区间以及返回翻转后的链表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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->NULLm = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:
Given mn satisfy the following condition:
1 ≤ m ≤ n ≤ length of list.

https://leetcode.com/problems/reverse-linked-list-ii/

 

 


 

 

翻转从m到n的链表。

翻转的操作还是和上一题一样: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} head
10  * @param {number} m
11  * @param {number} n
12  * @return {ListNode}
13  */
14 var reverseBetween = function(head, m, n) {
15     var res = new ListNode(-1), subHead = res, count = 1;
16     res.next = head;
17     while(count !== m){
18         subHead = head;
19         head = head.next;
20         count++;
21     }
22     var subTail = head, tmp;
23     while(count !== n + 1){
24         tmp = head.next;
25         head.next = subHead.next;
26         subHead.next = head;
27         head = tmp;
28         count++;
29     }
30     subTail.next = head;
31     return res.next;
32 };

 

 

 

转载于:https://www.cnblogs.com/Liok3187/p/4852361.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值