92. Reverse Linked List II【Medium】
92. Reverse Linked List II【Medium】
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.
解法一:
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if (head == NULL || head->next == NULL) {
return head;
}
ListNode * dummy = new ListNode(INT_MIN);
dummy->next = head;
ListNode * mth_prev = findkth(dummy, m - );
ListNode * mth = mth_prev->next;
ListNode * nth = findkth(dummy, n);
ListNode * nth_next = nth->next;
nth->next = NULL;
reverseList(mth);
mth_prev->next = nth;
mth->next = nth_next;
return dummy->next;
}
ListNode *findkth(ListNode *head, int k)
{
for (int i = ; i < k; i++) {
if (head == NULL) {
return NULL;
}
head = head->next;
}
return head;
}
ListNode * reverseList(ListNode * head)
{
ListNode * pre = NULL;
while (head != NULL) {
ListNode * next = head->next;
head->next = pre;
pre = head;
head = next;
}
return pre;
}
};

解法二:
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if (head == NULL || head->next == NULL) {
return head;
}
ListNode * dummy = new ListNode(INT_MIN);
dummy->next = head;
head = dummy;
for (int i = ; i < m; ++i) {
if (head == NULL) {
return NULL;
}
head = head->next;
}
ListNode * premNode = head;
ListNode * mNode = head->next;
ListNode * nNode = mNode;
ListNode * postnNode = mNode->next;
for (int i = m; i < n; ++i) {
if (postnNode == NULL) {
return NULL;
}
ListNode * temp = postnNode->next;
postnNode->next = nNode;
nNode = postnNode;
postnNode = temp;
}
mNode->next = postnNode;
premNode->next = nNode;
return dummy->next;
}
};

解法三:
public ListNode reverseBetween(ListNode head, int m, int n) {
if(head == null) return null;
ListNode dummy = new ListNode(); // create a dummy node to mark the head of this list
dummy.next = head;
ListNode pre = dummy; // make a pointer pre as a marker for the node before reversing
for(int i = ; i<m-; i++) pre = pre.next;
ListNode start = pre.next; // a pointer to the beginning of a sub-list that will be reversed
ListNode then = start.next; // a pointer to a node that will be reversed
// 1 - 2 -3 - 4 - 5 ; m=2; n =4 ---> pre = 1, start = 2, then = 3
// dummy-> 1 -> 2 -> 3 -> 4 -> 5
for(int i=; i<n-m; i++)
{
start.next = then.next;
then.next = pre.next;
pre.next = then;
then = start.next;
}
// first reversing : dummy->1 - 3 - 2 - 4 - 5; pre = 1, start = 2, then = 4
// second reversing: dummy->1 - 4 - 3 - 2 - 5; pre = 1, start = 2, then = 5 (finish)
return dummy.next;
}
参考了@ardyadipta 的代码,Simply just reverse the list along the way using 4 pointers: dummy, pre, start, then
92. Reverse Linked List II【Medium】的更多相关文章
- 82. Remove Duplicates from Sorted List II【Medium】
82. Remove Duplicates from Sorted List II[Medium] Given a sorted linked list, delete all nodes that ...
- 92. 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】92. Reverse Linked List II 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- [LeetCode] 92. Reverse Linked List II 倒置链表之二
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- [LeetCode] 92. Reverse Linked List II 反向链表II
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
- 【leetcode】92. 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】#92. Reverse Linked List II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...
- 【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List
The task is reversing a list in range m to n(92) or a whole list(206). All in one : U need three poi ...
- leetcode 92 Reverse Linked List II ----- java
Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Given 1-> ...
随机推荐
- golang技巧-接口型函数
接口型函数:指的是用函数实现接口,这样在调用的时候就会非常简便,这种函数为接口型函数,这种方式适用于只有一个函数的接口. 定义一个类型,这个类型只定义了函数的参数列表,函数参数列表与接口定义的方法一致 ...
- 【C语言】 Linux下编译提示pow未定义引用
如下代码: #include <stdio.h> // 调用基本输入输出函数库 #include <math.h> #define PI 3.14 // 定义常量 float ...
- MythXinWCF通用宿主绿色版V1.1
更新内容:宿主的唯一编号和名称可以输入符号"."日志文本框增加滚动条,并且总是显示文本末端增加启动方式选择:1.手动启动 2.跟随系统启动 最新下载地址: http://pan.b ...
- 对list_entry(ptr, type, member)的理解
如何根据一个结构体成员的地址.结构体类型以及该结构体成员名获得该结构体的首地址? #define list_entry(ptr, type, member) \ ((type *)((char *)( ...
- HTTP协议下保证登录密码不被获取最健壮方式
原文:http://www.cnblogs.com/intsmaze/p/6009648.html HTTP协议下保证登录密码不被获取最健壮方式 说到在http协议下用户登录如何保证密码安全这个问 ...
- Pressed状态和clickable,duplicateParentState的关系
做Android开发的人都用过Selector,可以方便的实现View在不同状态下的背景.不过,相信大部分开发者遇到过和我一样的问题,本文会从源码角度,解释这些问题. 首先,这里简单描述一下,我遇到的 ...
- windows SFC(System File Checker) 命令的使用
SFC(System File Checker)可以扫描所有受保护的系统文件的完整性,并使用正确的 Microsoft 版本替换. 步骤:点击开始,输入cmd: 右键,以管理员身份运行 输入sfc/s ...
- Solr6.6.0 用 SimplePostTool索引文件的启示
本文主要是介绍通过SimplePostTool工具索引文件的结果进行确认,针对不同的文件,索引的结果不同. 1.创建core 首先启动solr,建立名称为data的core,SimplePostToo ...
- solr6.6 导入 pdf/doc/txt/json/csv/xml文件
文本主要介绍通过solr界面dataimport工具导入文件,包括pdf.doc.txt .json.csv.xml等文件,看索引结果有什么不同.其实关键是managed-schema.solrcon ...
- Spark(七) -- Scala快速入门
Scala作为Spark的开发语言,想要成为Spark高手,精通Scala是必须要走的一条路 然后一门语言并不是你想精通就能够精通的,更何况是Scala这种面向对象又面向函数的编程语言,个人觉得其学习 ...