当前位置: 首页 > news >正文

[LeetCode]Reverse Linked List II

题目:Reverse Linked List II

逆置链表m到n的之间的节点。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        if (head == NULL || head->next == NULL ||m == n)return head;
        int count = 0;
        ListNode *p = head;
        ListNode *pre = NULL;
        while (count < m - 1){//找到m的节点的位置
            pre = p;
            p = p->next;
            count++;
        }
        ListNode *start = p;
        while (count < n - 1){//找到n的节点的位置
            p = p->next;
            count++;
        }
        reverseRecursion(start,p);
        if (pre == NULL){
            return p;
        }
        pre->next = p;
        return head;
    }
private:
    void reverseRecursion(ListNode *p,ListNode *end){//递归的方法逆置链表p
        ListNode *q;
        if (p->next == end){//倒数第二个节点
            q = end->next;
            p->next->next = p;
            p->next = q;
            return;
        }
        reverseRecursion(p->next, end);
        q = p->next->next;//逆置当前节点
        p->next->next = p;
        p->next = q;
    }
};

能用递归就能用栈来实现,

用栈来逆置链表,做法如下:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        if (head == NULL || head->next == NULL ||m == n)return head;
        int count = 0;
        ListNode *p = head;
        ListNode *pre = NULL;
        while (count < m - 1){
            pre = p;
            p = p->next;
            count++;
        }
        ListNode *start = p;
        while (count < n - 1){
            p = p->next;
            count++;
        }
        if (pre == NULL){
            return reverseNonRecursion(pre, start, p);
        }else{
            reverseNonRecursion(pre,start,p);
            return head;
        }
    }
private:
    ListNode *reverseNonRecursion(ListNode *pre, ListNode *p, ListNode *end){
        stack<ListNode *>stackLN;
        ListNode *q = p;
        while (q != end){//入栈
            stackLN.push(q);
            q = q->next;
        }
        end = end->next;
        if(pre == NULL)pre = q;//得到前一个节点
        else pre->next = q;
        while (!stackLN.empty()){
            p = stackLN.top();
            stackLN.pop();
            q->next = p;//逆置
            q = q->next;
        }
        q->next = end;
        return pre;
    }
};

直接用循环来逆置链表:

1->2->3->4->5;

(m,n)=(2,4);

p=2;pre=1;end=4;

q=5;

1)temp=p;

2)temp->next=q;//2->5

3)p=p->next;//p=3;

4)q=temp;//q=2;

循环1到4步

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* reverseBetween(ListNode* head, int m, int n) {
        if (head == NULL || head->next == NULL ||m == n)return head;
        int count = 0;
        ListNode *p = head;
        ListNode *pre = NULL;
        while (count < m - 1){
            pre = p;
            p = p->next;
            count++;
        }
        ListNode *start = p;
        while (count < n - 1){
            p = p->next;
            count++;
        }
        if (pre == NULL){
            return reverseNonRecursion2(pre, start, p);
        }else{
            reverseNonRecursion2(pre,start,p);
            return head;
        }
    }
private:
    ListNode *reverseNonRecursion2(ListNode *pre, ListNode *p, ListNode *end){
        ListNode *q = end->next;
        while (p != end){
            ListNode *temp = p;
            p = p->next;
            temp->next = q;
            q = temp;
        }
        p->next = q;
        if (pre == NULL)return p;
        pre->next = p;
        return pre;
    }
};

 

转载于:https://www.cnblogs.com/yeqluofwupheng/p/6685704.html

相关文章:

  • Vijos 1067Warcraft III 守望者的烦恼
  • 清除连接(其他电脑的)记录
  • 袋鼠云助力光伏产业 | 基于阿里云数加平台做算法预测
  • 重作了一次学生
  • 第十九章,指针小练习(C++)
  • jqgrid 定义方法, 而不向服务器请求
  • 售前工程师的成长---一个老员工的经验之谈(1)转载
  • IP路由原理
  • WinForm 下的 Wizard(向导) 控件, 提供设计时支持!
  • Logback手冊 Chapter 1: Introduction
  • [转]使用控件的RenderControl()方法导出Excel
  • 在移动端禁用长按选中文本功能
  • TNS-01190: The user is not authorized to execute the requested listener command
  • http://book.chinaz.com/database/sqlanywhere/ulcpzh9/ulcpzh9.htm
  • 基于Excel参数化你的Selenium2测试-xlrd
  • 5、React组件事件详解
  • chrome扩展demo1-小时钟
  • CSS中外联样式表代表的含义
  • Fundebug计费标准解释:事件数是如何定义的?
  • javascript面向对象之创建对象
  • js正则,这点儿就够用了
  • RxJS: 简单入门
  • SQLServer之索引简介
  • underscore源码剖析之整体架构
  • vue总结
  • Zepto.js源码学习之二
  • 测试如何在敏捷团队中工作?
  • 从零开始学习部署
  • 关于List、List?、ListObject的区别
  • 回顾 Swift 多平台移植进度 #2
  • 基于axios的vue插件,让http请求更简单
  • 我的面试准备过程--容器(更新中)
  • 最简单的无缝轮播
  • 蚂蚁金服CTO程立:真正的技术革命才刚刚开始
  • ​​​​​​​Installing ROS on the Raspberry Pi
  • ​LeetCode解法汇总2696. 删除子串后的字符串最小长度
  • #NOIP 2014# day.1 T3 飞扬的小鸟 bird
  • $.extend({},旧的,新的);合并对象,后面的覆盖前面的
  • (day 2)JavaScript学习笔记(基础之变量、常量和注释)
  • (HAL)STM32F103C6T8——软件模拟I2C驱动0.96寸OLED屏幕
  • (ZT)薛涌:谈贫说富
  • (附源码)springboot车辆管理系统 毕业设计 031034
  • (七)理解angular中的module和injector,即依赖注入
  • (三)Pytorch快速搭建卷积神经网络模型实现手写数字识别(代码+详细注解)
  • (一)Mocha源码阅读: 项目结构及命令行启动
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • *p=a是把a的值赋给p,p=a是把a的地址赋给p。
  • .[hudsonL@cock.li].mkp勒索病毒数据怎么处理|数据解密恢复
  • .net core IResultFilter 的 OnResultExecuted和OnResultExecuting的区别
  • .net core 客户端缓存、服务器端响应缓存、服务器内存缓存
  • .NET Standard 的管理策略
  • .net 打包工具_pyinstaller打包的exe太大?你需要站在巨人的肩膀上-VC++才是王道
  • .net操作Excel出错解决
  • .NET建议使用的大小写命名原则
  • .net快速开发框架源码分享