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.
Example:
Input: 1->2->3->4->5->NULL, m = 2, n = 4
Output: 1->4->3->2->5->NULL
根据经验,先建立一个虚结点dummy node,连上原链表的头结点,这样的话就算头结点变动了,我们还可以通过dummy->next来获得新链表的头结点。
步骤一:找到要倒置的链表起始位置的前一个,记为pre。
步骤二:从m-n倒置,移位m-n次,每一次cur的位置不变,利用临时结点t记录cur的下一个位置t=cur->next,t是即将倒置的下一个结点,故将cur的next指向t的下一个结点cur->next=t->next,t结点放置在发生倒置的起始位置,也就是t的next指向pre的next,t->next=pre->next,将pre的next指向t。
步骤三:返回虚结点的next。

方法一:(C++)
class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
ListNode* dummy=new ListNode(-1),* pre=dummy;
pre->next=head;
int index=0;
while(pre){
if(index==m-1)
break;
index++;
pre=pre->next;
}
ListNode* cur=pre->next;
for(int index=m;index<n;index++){
ListNode* t=cur->next;
cur->next=t->next;
t->next=pre->next;
pre->next=t;
}
return dummy->next;
}
};
LeetCode 92. Reverse Linked List II倒置链表2 C++的更多相关文章
- [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反转链表2
Reverse a linked list from position m to n. Do it in one-pass. Note: 1 ≤ m ≤ n ≤ length of list. Exa ...
- [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 翻转单链表II
对于链表的问题,根据以往的经验一般都是要建一个dummy node,连上原链表的头结点,这样的话就算头结点变动了,我们还可以通过dummy->next来获得新链表的头结点.这道题的要求是只通过一 ...
- [LeetCode]92. Reverse Linked List II反转部分链表
/* 重点还是反转链表 思路就是中间的反转,然后两头接上 */ public ListNode reverseBetween(ListNode head, int m, int n) { if (he ...
- 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-> ...
- 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 ...
- 92. Reverse Linked List II 反转链表 II
网址:https://leetcode.com/problems/reverse-linked-list-ii/ 核心部分:通过a.b.c三个变量之间的相互更新,不断反转部分链表 然后将反转部分左右两 ...
随机推荐
- 最新版的Chrome如何设置网页编码
在最新的V55版本中已经没有了编码选项,没有了这个可能会导致一些特殊编码网页出现乱码问题.那么如何找回这个Chrome的编码功能?可以通过下载chrome扩展:Set Character Encodi ...
- 关于在Servlet中的Fileter
Servlet(Server Applet)是Java Servlet的简称,称为小服务程序或服务连接器,用Java编写的服务器端程序,具有独立于平台和协议的特性,主要功能在于交互式地浏览和生成数据, ...
- Linux第三节课学习笔记
常见执行Linux命令的格式:命令名称 [命令参数] [命令对象]. 命令参数分长格式与短格式,短格式之间可合并. echo命令用于在终端输出字符串或变量提取后的值,格式为“echo [字符串 | $ ...
- Assembly Experiment9
用英文写太浪费时间了,而且书上的讲解对各种功能的英文原句少之又少,有空还是看龙书吧(不存在的) 实验1: 十六进制转换十进制 实验代码: ; 在屏幕上输出内存单元中的十进制两位数 assume cs: ...
- ARM Cortex M0 程序映像和启动流程
- java web(五):java web三大组件之另外两个和八大监听器
java的三大组件指Servlet.Filter.Listener.八大监听器指八个接口.前面介绍了Servlet,现在介绍一下Filter拦截器以及拦截地址的设置, Listener监听那些事件. ...
- 常用的HTML模板(转载)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- jumpserver管理入门
比较规范一点的步骤 1.先在用户管理查看用户组里,添加用户组 2.在用户管理查看用户里,添加用户 3.在资产管理里查看资产,添加资产.在批量增加那里可以使用表格快速导入添加 4.在授权管理里的系统用户 ...
- jquery: 获取当前天加减一天
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...
- python 数字以及字符串(方法总结,有的可能理解错误)
数字类型(int): 在python 2中,数字类型可以分为整形,长整形,浮点型,以及复数.在python3中都是整形和长整形都称之为整形,且python3中没有限制. 1.int方法使用,用于转换字 ...