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->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) return head;
ListNode fakehead = new ListNode(0);
fakehead.next = head;
ListNode pre = fakehead; for (int i = 0; i < m-1;i++) pre = pre.next; ListNode start = pre.next;
ListNode then = start.next; for(int i = 0 ;i < n -m ;i++){
start.next= then.next;
then.next = pre.next;
pre.next = then;
then=start.next;
}
return fakehead.next;
}
}
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 ...
- [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-> ...
- 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 解题报告(Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 题目地址:https://leet ...
- LeetCode 92. Reverse Linked List II倒置链表2 C++
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反转链表2
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- 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 ...
- [LeetCode]92. Reverse Linked List II反转部分链表
/* 重点还是反转链表 思路就是中间的反转,然后两头接上 */ public ListNode reverseBetween(ListNode head, int m, int n) { if (he ...
随机推荐
- Oracle 用户解锁
ALTER USER hr ACCOUNT UNLOCK ALTER USER hr IDENTIFIED BY welcome
- php 关于日期的知识总结
1.UNIX时间戳 time() echo time(); 2.UNIX时间戳转换为日期用函数: date() 一般形式:date(); 即 echo date(date('Y-m-d H:i:s ...
- spring aop 样例
基于注解的AOP 方式 1.加入jar包 com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver- ...
- yield方法
yield方法的作用是房企当前的CPU资源,将他让给其他的任务去占用CPU执行时间,但房企的时间不确定,有可能刚刚放弃,马上又获得CPU时间片. package yield; /** * Create ...
- 原创Java多线程详解(一)
只看书不实践是不行的.来实践一下~~~~~~(引用请指明来源) 先看看百科对多线程的介绍 多线程(英语:multithreading),是指从软件或者硬件上实现多个线程并发执行的技术.具有多线程能力的 ...
- Python语音合成
注意:通过win32com调用的windows的SAPI,所以本脚本只适应于windows平台 代码很简单 #coding:utf-8 import win32com.client import ti ...
- 从零打造在线网盘系统之Hibernate配置O/R映射
欢迎浏览Java工程师SSH教程从零打造在线网盘系统系列教程,本系列教程将会使用SSH(Struts2+Spring+Hibernate)打造一个在线网盘系统,本系列教程是从零开始,所以会详细以及着重 ...
- ibatis 中#和 $ 符号的区别
1.数据类型匹配 #:会进行预编译,而且进行类型匹配(自动确定数据类型): $:不进行数据类型匹配. 2.实现方式: # 用于变量替换(先生成一个占位符,然后替换) select * from use ...
- .NET获取Html字符串中指定标签的指定属性的值
using System.Text; using System.Text.RegularExpressions; //以上为要用到的命名空间 /// <summary> /// 获取Htm ...
- android.os.Handler
android.os.handler A Handler allows you to send and process Message and Runnable objects associated ...