Leetcode92: Reverse Linked List II 翻转链表问题
问题描述
给定一个链表,要求翻转其中从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 翻转链表问题的更多相关文章
- lintcode 中等题: reverse linked list II 翻转链表II
题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4-> ...
- 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 ...
- Leetcode92. Reverse Linked List II反转链表
反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明: 1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5->NULL, m = 2 ...
- [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] 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-> ...
- 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 92] Reverse Linked List II 翻转单链表II
对于链表的问题,根据以往的经验一般都是要建一个dummy node,连上原链表的头结点,这样的话就算头结点变动了,我们还可以通过dummy->next来获得新链表的头结点.这道题的要求是只通过一 ...
- [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- ...
随机推荐
- Spring(Bean)1
Spring支持3种依赖注入的方式 (DI依赖注入)*属性注入 (配置bean set方法注入) <bean id="car" class="spring.bean ...
- java数据类型(大小等),变量定义,各进制书写方法
1. java中字符占两个字节,因为char类型占两个字节(16位),而C,C++中占1字节(8位). 2. 变量定义 第一步:声明(Declaration) 第二步:赋值(Assignment) 这 ...
- 重写系统自带tabbar出现的 代理错误
- http://t.cn/xxxxx的短链接如何生成?
var convertStr = encodeURIComponent(urlStr); //转换的原链接可能存在"&"这样的特殊符号,导致原链接的某些字段会被当做ajax ...
- Spring IOC容器装配Bean_基于注解配置方式
bean的实例化 1.导入jar包(必不可少的) 2.实例化bean applicationContext.xml(xml的写法) <bean id="userDao" cl ...
- 个性的圆角.html
- jenkins的部署
一.jenkins相关工具安装 基于tomcat安装iptables -F setenforce 0 systemctl stop firewalld.service Jdk安装 wgt htt ...
- 大数据之Linux基础
回顾这一个多月以来闭关学大数据的一些相关重要知识,就当复习,顺便以备以后查看 Linux学习第一步自然是安装Linux. 关于Linux 首先介绍下Linux,Linux系统很多程序员开发者其实都耳熟 ...
- 强化学习环境OpenAi搭建,从虚拟机到Gym、Mujoco和mujoco-py的完整安装
平时不怎么写博客,这次是因为环境的配置花费了我大概一个星期的时间.所以简单的记录一下搭建的整个过程,其中有些部分我直接推荐别人的博客的基本教程,都是我亲自尝试过成功的.同时,也希望这篇博客可以帮到您. ...
- Selenium选择web元素
获取html片段可以用来做什么? 可以用来分割,也可以分析HTML文档 beautifulsoup用法? 安装beautifulsoup库: pip install beautifulsoup4 因为 ...