[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.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
题意:
给定一个链表,反转第m~n个节点。
反转链表的一般思路

Solution1:
1.用指针找到m和n位置
2.反转m和n之间的链表


code
class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if(head==null) return head;
ListNode dummy = new ListNode(-1);
dummy.next = head;
ListNode mNode = head;
ListNode preM = dummy;
ListNode nNode = head;
for (int i = 1; i < m ; i++) {
preM = mNode;
mNode = mNode.next;
}
for (int i = 1; i <n ; i++) {
nNode = nNode.next;
}
while(mNode != nNode){
preM.next = mNode.next;
mNode.next = nNode.next;
nNode.next = mNode;
mNode = preM.next;
}
return dummy.next;
}
}
[leetcode]92. Reverse Linked List II反转链表2的更多相关文章
- [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反转部分链表
/* 重点还是反转链表 思路就是中间的反转,然后两头接上 */ public ListNode reverseBetween(ListNode head, int m, int n) { if (he ...
- [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倒置链表2 C++
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
网址:https://leetcode.com/problems/reverse-linked-list-ii/ 核心部分:通过a.b.c三个变量之间的相互更新,不断反转部分链表 然后将反转部分左右两 ...
- [Leetcode] 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来获得新链表的头结点.这道题的要求是只通过一 ...
- 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 206 Reverse Linked List(反转链表)(Linked List)(四步将递归改写成迭代)(*)
翻译 反转一个单链表. 原文 Reverse a singly linked list. 分析 我在草纸上以1,2,3,4为例.将这个链表的转换过程先用描绘了出来(当然了,自己画的肯定不如博客上面精致 ...
随机推荐
- mysql ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constrain fails
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constrain fails. 可能是MySQL在In ...
- mvc部分视图转换成html字符串
public static class RenderViewTostring { /// <summary> ///将部分视图转成html 字符串方便我们扩展使用 /// </sum ...
- Python3.6.2安装pip install paramike模块报错
问题描述: 在有几台电脑上pip install paramike报错 报错内容: Could not find a version that satisfies the requirement sq ...
- REST API TO MiniProgram 上线WordPress官方插件库
全新开发的用于 wordpress微信小程序的插件 REST API TO MiniProgram今天上线WordPress官方插件库.这个插件的上一个版本叫:wp-rest-api-for-app, ...
- arrayList转换为数据
ArrayList arrayList = SetTools.loadfile(path); string[] str = (string[])arrayList.ToArray(typeof(str ...
- GNU C和C99标准中的可变参数宏(variadic macros)
用可变参数宏(variadic macros)传递可变参数表你可能很熟悉在函数中使用可变参数表,如: void printf(const char* format, …); 直到最近,可变参数表还是只 ...
- 前三次OO作业小结
I used to be enamored of object-oriented programming. I'm now finding myself leaning toward believin ...
- pandas数据结构之series操作
阅读之前假定你已经有了python内置的list和dict的基础.这里内容几乎是官方文档的翻译版本. 概览: 原来的文档是在一个地方,那边的代码看起来舒服些 https://www.y ...
- 【C语言基础】循环体系
1.For循环结构: For循环的一般形式为: for (表达式1 初始化:判断条件:自增自减) { 语句块 } 2.while循环结构: while循环的一般的形式为: 表达式1 初始化 while ...
- 6Linux用户身份与文件权限
3类用户身份: (1)管理员UID为0,root (2)系统用户UID为1-999:nologin不能登录系统,老版本5.6中是1-499 (3)普通用户UID为1000开始,老版本5.6中是1000 ...