36-翻转链表 II

翻转链表中第m个节点到第n个节点的部分

注意事项

m,n满足1 ≤ m ≤ n ≤ 链表长度

样例

给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4->3->2->5->null

挑战

在原地一次翻转完成

标签

链表

思路

借助2个空的节点,置于链表头部,一次遍历链表,找到翻转子链尾部subEnd,不翻转子链尾部lastEnd,头部nextHead,以及当前节点nowNode,在遍历时,将nowNode插入lastEnd与subEnd之间。最后,将翻转子链与nextHead相连。

code

/**
* Definition of singly-linked-list:
*
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param head: The head of linked list.
* @param m: The start position need to reverse.
* @param n: The end position need to reverse.
* @return: The new head of partial reversed linked list.
*/
ListNode *reverseBetween(ListNode *head, int m, int n) {
// write your code here ListNode newHead1(-1), newHead2(-1);
newHead1.next = &newHead2;
newHead2.next = head; ListNode *nowNode=head, *subHead=head, *subEnd=NULL, *lastEnd=&newHead1, *nextHead=NULL;
int i=1;
while(nowNode != NULL) {
if(i <= m) {
lastEnd = lastEnd->next;
subEnd = nowNode;
nextHead = nowNode;
nowNode = nowNode->next;
i++;
}
else if(i <= n) {
ListNode *temp = nowNode->next;
nextHead->next = NULL;
lastEnd->next = nowNode;
lastEnd->next->next = subEnd;
subEnd = nowNode;
nowNode = temp;
i++;
}
else {
nextHead->next = nowNode;
break;
}
} if(m == 1) {
return lastEnd->next;
}
else {
return head;
}
}
};

lintcode-36-翻转链表 II的更多相关文章

  1. lintcode 中等题: reverse linked list II 翻转链表II

    题目 翻转链表 II 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4-> ...

  2. lintcode: 翻转链表

    题目: 翻转链表 翻转一个链表 样例 给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null 挑战 在原地一次翻转完成 解题: 递归还 ...

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

  4. LeetCode 92 | 大公司常考的面试题,翻转链表当中指定部分

    今天是LeetCode专题的第58篇文章,我们一起来看看LeetCode 92题,翻转链表II(Reverse LInked List II). 这题的官方难度是Medium,2451个赞同,145个 ...

  5. [LintCode] Reverse Nodes in k-Group 每k个一组翻转链表

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  6. [LeetCode系列]翻转链表问题II

    给定一个链表和两个整数m, n, 翻转链表第m个节点到第n个节点(从1开始计数). 如, 给定链表: 1->2->3->4->5->NULL, 以及 m = 2, n = ...

  7. [LeetCode] Reverse Nodes in k-Group 每k个一组翻转链表

    Given a linked list, reverse the nodes of a linked list k at a time and return its modified list. If ...

  8. C语言递归,非递归实现翻转链表

    翻转链表作为,链表的常用操作,也是面试常遇到的. 分析非递归分析: 非递归用的小技巧比较多,很容易出错. 递归分析比较简单,在代码里面 代码: #include<stdio.h> #inc ...

  9. 025k个一组翻转链表

    #include "000库函数.h" struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), n ...

随机推荐

  1. mvc 页面 去掉转义字符

    mvc 页面 去掉转义字符   mvc 后台返回json数据,用ViewBag 传回前台页面,但是传到前台页面的时候,带有转义字符.一直想去掉这个转义字符,苦恼了好久. 解决方案: mvc 页面有个这 ...

  2. Java : Netty 入门案例

    接收端代码: public class IOServer { public static void main(String[] args) throws IOException, Interrupte ...

  3. Oracle之多表查询

    -多表查询 1.交叉连接 select * from t_class for update; select * from t_student for update; select for update ...

  4. Linux3.5—IIC学习分析

    I2C控制器的设备对象内核已经实现并关联到platform总线. I2C控制器的驱动对象内核已经实现. 看mach-tiny4412.h /plat-samsung/目录下 /drivers/i2c/ ...

  5. 用树莓派做电视盒子,安装Android TV系统

    有位朋友问我,如何在树莓派上安装盒子系统,这期我就教大家如何安装Android系统,自动动手做一个机顶盒. 如何安装系统,我已经在 树莓派安装系统 这篇文章中了做介绍,有需要的请看这篇文章.安装系统需 ...

  6. rails应用无法读取kafka数据报错Kafka::Error: Failed to find group coordinator

    如果确保kafka中有数据,rails应用中却无法读取到,或报如下错误: Kafka::Error: Failed to find group coordinator   一般有两种情况,解决:   ...

  7. Java垃圾回收机制概述

    总览 本文会介绍垃圾回收的以下几个方面. 为什么要垃圾回收 在哪里回收 哪些对象需要回收 怎么回收 HotSpotJVM中有哪些具体的回收器可以直接用. 在开始讲垃圾回收之前,先通过一张图快速回忆一下 ...

  8. 前端chrome调试技巧

    待更新:http://blog.csdn.net/xueer767/article/details/65936204?locationNum=8&fps=1

  9. dvs-panotracking编译运行

    编译运行dvs-panotracking > 编译dvs-panotracking之前首先需要安装imageutilities . 源码下载 https://github.com/VLOGrou ...

  10. beego 点滴

    在使用beego时遇到 need a primary key field 1 确保结构中的 字段首字母大写 2 beego默认主键是id 如果主键定义的是其他字段比如userid 那么加上orm pk ...