[LC] 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.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
ListNode dummy = new ListNode(-1);
dummy.next = head; ListNode cur1 = dummy;
ListNode pre1 = null;
for (int i = 0; i < m; i++) {
pre1 = cur1;
cur1 = cur1.next;
} ListNode cur2 = cur1;
ListNode pre2 = pre1;
ListNode nxt = null;
for(int i = m; i <= n; i++) {
nxt = cur2.next;
cur2.next = pre2;
pre2 = cur2;
cur2 = nxt;
} // connect 1 -> 4
pre1.next = pre2;
// connnect 2 -> 5
cur1.next = cur2; return dummy.next;
}
}
[LC] 92. Reverse Linked List II的更多相关文章
- 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 ...
- 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 倒置链表之二
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 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- 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-> ...
- LeetCode OJ 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
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 ...
随机推荐
- DDL&DML
SQL语言的DDL和DML DDL:数据定义语言它是用来定义 管理数据对象(数据库和数据表)的. create(创建),alter(修改),drop(删除) 一.创建数据库create datebas ...
- MySQL--SHOW ENGINE INNODB STATUS
===================================== -- :: 0x7f305b965700 INNODB MONITOR OUTPUT =================== ...
- 计算机网络(7): 传输层TCP和UDP以及TCP的工作方式
UDP:无连接:不保证可靠:面向报文的: TCP:面向连接:提供可靠交付:面向字节流(把应用层的数据分包,每个包装一些字节:不关心应用层给的包多大,而是根据网络状况,窗口大小决定) TCP报文: 序号 ...
- Python笔记_第四篇_高阶编程_正则表达式_1.正则表达式简介(re模块)
1. 从一个判断手机号的问题引入: 如果给你一个字符串,去判断是否是一个手机号码,我们通过之前的学习可以有如下代码: # 如果用普通的方式去检验一个电话号码非常麻烦. def checkPhone(s ...
- 84.常用的返回QuerySet对象的方法使用详解:select_related, prefetch_related
1.select_related: 只能用在一对多或者是一对一的关联模型之间,不能用在多对多或者是多对一的关联模型间,比如可以提前获取文章的作者,但是不能通过作者获取作者的文章,或者是通过某篇文章获取 ...
- 用一行Python进行数据收集探索
简易的Pandas之路 任何使用Python数据的人都会熟悉Pandas包.Pandas是大多数行和列格式数据的go-to包.如果你没有Pandas,请确保在终端中使用pip install安装: p ...
- MyBatis从入门到精通(第5章):MyBatis代码生成器
jdk1.8.MyBatis3.4.6.MySQL数据库5.6.45.Eclipse Version: 2019-12 M2 (4.14.0) MyBatis从入门到精通(第5章):MyBatis代码 ...
- JS—DOM操作
节点分为三类: 1.元素节点:标签<div></div> 2.文本节点:标签内的纯文本. 3.属性节点:标签内的属性,id或class 查找元素: getElementById ...
- Problem for Nazar CodeForces - 1151C (前缀和)
Problem for Nazar Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for ...
- IT人员职业发展规划