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-> ...
随机推荐
- POJ 1113 Wall(凸包)
[题目链接] http://poj.org/problem?id=1113 [题目大意] 给出一个城堡,要求求出距城堡距离大于L的地方建围墙将城堡围起来求所要围墙的长度 [题解] 画图易得答案为凸包的 ...
- tomcat官网
http://tomcat.jaxmao.org/appdev/index.html 配置 http://www.cnblogs.com/starhu/p/5599773.html
- Java读取文本文件
try { // 防止文件建立或读取失败,用catch捕捉错误并打印,也可以throw StringBuilder stringBuilder = new StringBuilder(); // 读入 ...
- 《西安交大电路》(Principles of Electrical Circuits) 学习笔记
内容简介:电路分析是电子类专业的第一门基础课. 电路理论包括电路分析和电路综合两大方面内容.电路分析的主要内容是指在给定电路结构.元件参数的条件下,求取由输入(激励)所产生的输出(响应):电路综合则主 ...
- android 用 XML 自定义View边框个数,只有一边或两边
<?xml version="1.0" encoding="UTF-8"?> <layer-list xmlns:android=" ...
- 高精度整数 - a+b(王道)
题目描述: 实现一个加法器,使其能够输出a+b的值. 输入: 输入包括两个数a和b,其中a和b的位数不超过1000位. 输出: 可能有多组测试数据,对于每组数据,输出a+b的值 样例输入: 2 6 1 ...
- represent states with objects
1. The behavior of objects in the real world is more complex than simply being in one state at a tim ...
- 开源知识库管理系统选型 centos6.4 搭建knowlededgeroot-1.0.4知识库平台
开源知识库管理系统选型,除了使用wiki外,还有下面可选: http://www.knowledgebase-script.com/ https://github.com/lordlamer/know ...
- 云计算之路:AWS, Azure, Aliyun, UCloud提供的Windows操作系统
如果您用的是微软平台,如果您准备走上云计算之路,估计您首先关心的是云服务商有没有提供合适的Windows操作系统. 这里把我们知道的知名云服务商提供的Windows操作系统列出来,供大家参考. 1. ...
- vue - config
build/build.js -> config 详细的config配置走向.