[LeetCode] 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.
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} m
* @param {number} n
* @return {ListNode}
*/
var reverseBetween = function(head, m, n) {
var prev = null;
var now = head;
var next = head.next;
var iprev = null;
var inext = null;
var ihead = null;
var itail = null;
var s = 1; var newhead = false; while (now) {
if (s >= m && s <= n) {
if (s == m) {
itail = now;
iprev = prev;
}
if (s == n) {
ihead = now;
inext = next;
}
now.next = prev;
prev = now;
now = next;
if (now) {
next = now.next;
} if (s == n) {
if (!iprev) {
head = ihead;
} else {
iprev.next = ihead;
}
itail.next = inext;
break;
}
} else {
prev = now;
now = next;
if (now) {
next = now.next;
}
}
s++;
}
return head;
};
代码比较丑,用了大量的零时变量来存东西,思路跟翻转整个链表那题如出一辙。简述一下思路就是:用三指针prev, now, next 遍历链表,当到达指定范围后开始翻转操作,并顺便记录翻转那部分的头和尾(ihead, itail)还有相应接头部分的指针(iprev, inext)以便最后翻转完了以后把两部分接起来。特别注意的是链表的头可能会被改变。
[LeetCode] Reverse Linked List II的更多相关文章
- 【原创】Leetcode -- Reverse Linked List II -- 代码随笔(备忘)
题目:Reverse Linked List II 题意:Reverse a linked list from position m to n. Do it in-place and in one-p ...
- [LeetCode] 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-> ...
- [leetcode]Reverse Linked List II @ Python
原题地址:https://oj.leetcode.com/problems/reverse-linked-list-ii/ 题意: Reverse a linked list from positio ...
- [Leetcode] Reverse linked list ii 反转链表
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given1->2 ...
- leetcode——Reverse Linked List II 选择链表中部分节点逆序(AC)
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1-> ...
- LeetCode Reverse Linked List II 反置链表2
题意:将指定的一段位置[m,n]的链表反置,返回链表头. 思路:主要麻烦在链表头,如果要从链表头就开始,比较特殊. 目前用DFS实现,先找到m-1的位置,再找到n+1的位置,中间这段就是否要反置的,交 ...
- lc面试准备:Reverse Linked List II
1 题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1 ...
- LeetCode之“链表”:Reverse Linked List && Reverse Linked List II
1. Reverse Linked List 题目链接 题目要求: Reverse a singly linked list. Hint: A linked list can be reversed ...
- LeetCode 92. 反转链表 II(Reverse Linked List II)
92. 反转链表 II 92. Reverse Linked List II 题目描述 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. LeetC ...
随机推荐
- [webpack] devtool配置对比
文件结构 -src -views -essay -list.js -detail.js -index.js -webpack.config.js 文件内容 [/src/.../index.js] im ...
- gulp watch出现Error: watch null EPERM的问题解释
出现这样的问题,一般是第一次运行导致的,而且任务上有删除文件的操作. 我观察发现,只要把输出目录的文件删除,然后重新运行watch就一些ok,后者再运行一次gulp watch就一切正常.
- SpringBoot源码解析:tomcat启动分析
>> spring与tomcat的启动分析:war包形式 tomcat:xml加载规范 1.contex-param: 初始化参数 2.listener-class: contextloa ...
- java 对象入门
对象的五大特征 (1)所有东西都是对象.可将对象想象成一种新型变量;它保存着数据,但可要求对自身进行操作.理论上讲, 可从要解决的问题身上提出所有概念性的组件,然后再程序中将其表达为一个对象. (2) ...
- BUAA_OVERWATCH第一次行动前战略部署
这太IMBA了! 需求调研问卷的反馈 #define A 调查问卷 A设计背景 随着各种新兴手游的兴起,以及各大直播间内Lying Man的火热,以及各种娱乐方式的发展,传统桌游很好地移植到app上的 ...
- <<< java如何调用系统程序
通过 java.lang.Runtime 类用操作系统命令 然后调用run.exec()进程来执行命令程序 cmd /c dir 是执行完dir命令后关闭命令窗口. cmd /k dir 是执行完di ...
- XPath函数——字符串函数(转载)
本文是转载的,原文网址:http://www.cnblogs.com/zhaozhan/archive/2010/01/17/1650242.html 字符串函数主要用来处理字符串.字符串函数主要包括 ...
- NPOI 导出Excel
NPOIExcel npoiexcel = new NPOIExcel(); string filename = DateTime.Now.ToString("yyyyMMddHHmmssf ...
- ssh模仿ansible批量执行命令的功能
#!/bin/bash ssh_hosts=("IP" "IP1".......) user=root remote_cmd="df -h" ...
- [译]学习HTTP协议的请求行
原文:http://fiddler2.com/blog/blog/2013/02/13/understanding-the-request-line 最近有一位Fiddler用户问我一个问题: 我在使 ...