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++的更多相关文章

  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 ...

  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-> ...

  3. [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 ...

  4. [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-> ...

  5. [LeetCode 92] Reverse Linked List II 翻转单链表II

    对于链表的问题,根据以往的经验一般都是要建一个dummy node,连上原链表的头结点,这样的话就算头结点变动了,我们还可以通过dummy->next来获得新链表的头结点.这道题的要求是只通过一 ...

  6. [LeetCode]92. Reverse Linked List II反转部分链表

    /* 重点还是反转链表 思路就是中间的反转,然后两头接上 */ public ListNode reverseBetween(ListNode head, int m, int n) { if (he ...

  7. 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-> ...

  8. 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 ...

  9. 92. Reverse Linked List II 反转链表 II

    网址:https://leetcode.com/problems/reverse-linked-list-ii/ 核心部分:通过a.b.c三个变量之间的相互更新,不断反转部分链表 然后将反转部分左右两 ...

随机推荐

  1. introduce of servlet and filter

    servlet简介: Java Servlet 是运行在 Web 服务器或应用服务器上的程序,它是作为来自 Web 浏览器或其他 HTTP 客户端的请求和 HTTP 服务器上的数据库或应用程序之间的中 ...

  2. HTC Vive设备拥有陀螺仪。

    //设置设备陀螺仪的开启/关闭状态,使用陀螺仪功能必须设置为 true Input.gyro.enabled = true; //获取设备重力加速度向量 Vector3 deviceGravity = ...

  3. Linux第七节课学习笔记

    RHEL7用户身份有以下这些: 1.管理员 root UID:0 权限最大: 2.系统用户 UID:1-999: 3.普通用户 UID:1000+. 一个用户基本组只有一个,扩展组可多个,创建扩展组用 ...

  4. kotlin 编译 运行 hello world

    kotlin 编译器下载地址:https://github.com/JetBrains/kotlin/releases/tag/v1.3.31 解压:kotlin-compiler-1.3.31.zi ...

  5. winscp工具和xshell连接linux机器时切换到root账户

    由于工作中一些机器不能以root直接登陆(sshd_config配置了不能直接root登陆),但是又想连接的时候切换为root用户 处理方式 1.给普通用户sudo su - 权限 命令行输入visu ...

  6. MySQL innobackupex全量备份恢复

    转自 http://blog.itpub.net/27099995/viewspace-1295099/ 先简单介绍一下这个工具:innobackupexinnobackupex比xtarbackup ...

  7. python面试题--数据类型

    数据类型 字典 dict:字典,字典是一组键(key)和值(value)的组合,通过键(key)进行查找,没有顺序, 使用大括号”{}”;应用场景:dict,使用键和值进行关联的数据; 现有字典d={ ...

  8. idea调试代码跟踪到tomcat代码里面

    在POM.xml文件里面加上如下代码即可: <dependency> <groupId>org.apache.tomcat</groupId> <artifa ...

  9. 使用phpunit测试yaf项目操作步骤

    yaf + phpunit 使用phpunit对yaf进行测试的核心在于bootstrip文件的配置. *1. 首先在项目目录下创建tests文件,并在tests中创建phpunit.xml < ...

  10. 如何创建 SVN 服务器,并搭建自己的 SVN 仓库 如何将代码工程添加到VisualSVN Server里面管理

    如何创建 SVN 服务器,并搭建自己的 SVN 仓库,附链接: https://jingyan.baidu.com/article/6b97984dca0d9c1ca3b0bf40.html 如何将代 ...