问题描述

给定一个链表,要求翻转其中从m到n位上的节点,返回新的头结点。

Example

Input: 1->2->3->4->5->NULL, m = 2, n = 4

Output: 1->4->3->2->5->NULL

分析

这题解法比较直接,可以定义一个fakeNode, 首先拼接 1 到 m - 1的节点, 然后拼接链表 m 到 n reverse 之后的结果,最后拼接剩下的节点。reverse链表这里主要使用stack和3指针方法。

解法

方法1. Stack

 ListNode fakeHead = new ListNode(0);
ListNode cur =fakeHead;
ListNode curH = head;
int cnt = n - m + 1;
//链接 1到m - 1节点
while(m > 1){
cur.next = curH;
curH = curH.next;
cur = cur.next;
m--;
}
cur.next = null;
//链接m 到 n 翻转之后的结果
Stack<ListNode> stack =new Stack();
for(int i = 0; i < cnt; i++){
stack.push(curH);
curH = curH.next;
}
while(!stack.isEmpty()){
cur.next = stack.pop();
cur = cur.next;
}
//链接 n + 1 之后的链表
cur.next = curH; return fakeHead.next;

时间复杂度O(n),空间复杂度O(n)

方法2. 3指针

 public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode fakeHead = new ListNode(0);
ListNode cur =fakeHead;
ListNode curH = head;
int cnt = n - m + 1;
//链接 1到m - 1节点
while(m > 1){
cur.next = curH;
curH = curH.next;
cur = cur.next;
m--;
}
cur.next = null;
//链接m 到 n 翻转之后的结果
ListNode pre = null;
ListNode next = curH.next;
ListNode tail =curH;
for(int i = 0; i < cnt; i++){
curH.next = pre;
pre = curH;
curH = next;
if(next != null){
next = next.next;
}
}
cur.next = pre;
//链接 n + 1 之后的链表
tail.next = curH; return fakeHead.next;
}

时间复杂度O(n),空间复杂度O(1)

Leetcode92: Reverse Linked List II 翻转链表问题的更多相关文章

  1. lintcode 中等题: reverse linked list II 翻转链表II

    题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4-> ...

  2. 92. Reverse Linked List II 翻转链表II

    Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...

  3. Leetcode92. Reverse Linked List II反转链表

    反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5->NULL, m = 2 ...

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

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

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

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

  8. [LeetCode 92] Reverse Linked List II 翻转单链表II

    对于链表的问题,根据以往的经验一般都是要建一个dummy node,连上原链表的头结点,这样的话就算头结点变动了,我们还可以通过dummy->next来获得新链表的头结点.这道题的要求是只通过一 ...

  9. [LeetCode92]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- ...

随机推荐

  1. MySQL双日志

    InnoDB引擎的redo log日志 解决什么问题? 我们每次更新数据如果都要直接写到硬盘存储的话,如果更新数据频繁的话,整个过程的Io成本和查找成本都会很高(比方说每次启动磁盘,平均的寻找数据时间 ...

  2. c语言l博客作业02

    问题 答案 这个作业属于哪个课程 C语言程序设计l 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/SE2019-2/homework/8687 我在这个 ...

  3. Android Activity生命周期及启动模式

    曾经搞过许多东西,再熟练的东西一段时间没有碰都会生疏或忘记.后来体会到写成文档记录下来的重要性,但有些word或ppt记录下来的东西随着时间流逝会丢失,或者不愿去看.或许保存成博客的形式,会是更好的选 ...

  4. 走近深度学习,认识MoXing:初识华为云ModelArts的王牌利器 — MoXing

    [摘要] 本文为MoXing系列文章第一篇,主要介绍什么是MoXing,MoXing API的优势以及MoXing程序的基本结构. MoXing的概念 MoXing是华为云深度学习服务提供的网络模型开 ...

  5. 【我的物联网成长记8】超速入门AT指令集【华为云技术分享】

    [摘要] 在物联网中,AT命令集可用于控制&调测设备.通信模块入网等.本文为您介绍NB-IoT常用的AT命令集及其调测工具. 什么是AT指令集 AT命令,用来控制TE(Terminal Equ ...

  6. 用生动的案例一步步带你学会python多线程模块

    鱼和熊掌不可兼得 鱼,我所欲也,熊掌,亦我所欲也,二者不可得兼,舍鱼而取熊掌者也. 从6月开始写公众号,连着四个月一直尽量保证一周五更,结果整天熬夜搞的身体素质骤降.十一休假决定暂时将公众号放放,好好 ...

  7. JetBrains 迷你地图插件 CodeGlance

    JetBrains 本身不带迷你地图功能,但可以通过插件的形式来实现. 直接在 Settings 里边搜索 CodeGlance,安装后重启 IDE 就有了.

  8. Redux和Context对比

    Redux和Context对比 如果项目体量较小,只是需要一个公共的store存储state,而不讲究使用action来管理state,那context完全可以胜任.反之,则是redux的优点. co ...

  9. Apache + WordPress 从 0 搭建

    引言 又到周末了,周末小编一般不更新系列文章,原因嘛是因为要攒稿子,年底工作比较忙,不攒点稿子是要断更的:( 所以周末一般聊点简单轻松加愉快的东西,小编现在的博客站是由 HEXO 进行搭建的,最近随着 ...

  10. 2017 CCPC秦皇岛 G题 Numbers

    DreamGrid has a nonnegative integer . He would like to divide  into nonnegative integers  and minimi ...