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->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.

解法一:

 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】的更多相关文章

  1. 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 ...

  2. 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- ...

  3. 【LeetCode】92. Reverse Linked List II 解题报告(Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...

  4. [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 ...

  5. [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-> ...

  6. 【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-> ...

  7. 【一天一道LeetCode】#92. Reverse Linked List II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Reverse ...

  8. 【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 ...

  9. 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-> ...

随机推荐

  1. 【差分约束系统】【spfa】UVALive - 4885 - Task

    差分约束系统讲解看这里:http://blog.csdn.net/xuezhongfenfei/article/details/8685313 模板题,不多说.要注意的一点是!!!对于带有within ...

  2. spring MVC中获取request和response:

    spring MVC中获取request和response: HttpServletRequest request = ((ServletRequestAttributes) RequestConte ...

  3. python-sdk-demo的打包

    1.安装setuptools pip install python-setuptools 2.创建一个简单的包 下载demo https://github.com/cp-m/py-sdk-demo.g ...

  4. Golang优秀开源项目汇总, 10大流行Go语言开源项目, golang 开源项目全集(golang/go/wiki/Projects), GitHub上优秀的Go开源项目

    Golang优秀开源项目汇总(持续更新...)我把这个汇总放在github上了, 后面更新也会在github上更新. https://github.com/hackstoic/golang-open- ...

  5. nfs mount 故障 mount.nfs: access denied by server while mounting 10.0.100.208:/backup_usb

    生产环境: 服务端centos7.2,客户端:ubuntu16.04 挂载出现的故障: root@HDCtrl100:/mnt# mount -t nfs 10.0.100.208:/backup_u ...

  6. install libiconv libraries

    #下载地址: https://ftp.gnu.org/pub/gnu/libiconv/ #编译安装 ./configure --prefix=/usr/local make make install ...

  7. service 和 Controller 差别

    service  层能够看做是还有一个 DAO 层,仅仅是在里面封装了还有一些逻辑. 而 Controller 和 service 差别就大了.Controller 要处理请求映射, service ...

  8. hibernate hql 语句中 in 的用法

    例子描述查询一些班级中的学生 /** * * @param city * @return */ public List<Student> studentList(final Integer ...

  9. python 对比学习

    python和java面向对象的不同 1.属性和方法 java中类的属性(static)除外,对象全部独立拥有: 而python中类的属性,其实例对象一个字段都没有.底层是这么搞的: 对象object ...

  10. redis源代码分析(5)——aof

    前面几篇基本介绍了redis的主要功能.流程.接下来是一些相对独立的部分,首先看一下持久化. redis持久化支持两种方式:RDB和AOF,我们首先看一下AOF的实现. AOF(Append only ...