题目

Reverse a linked list from position m to n. Do it in-place and in one-pass.

For example:

Given 1->2->3->4->5->NULL, m = 2 and n = 4,

return 1->4->3->2->5->NULL.

Note:

Given m, n satisfy the following condition:

1 ≤ m ≤ n ≤ length of list.

分析

题目给定一个链表,以及两个整数,代表链表中节点的序列号,要求,将序列号之间的子链表反转,重新链接返回。

利用栈先进后出的思想,将要求反转的节点存入栈中,然后依次弹出,重新链接即可。

需要注意的是,要准确记录反转子链表的前一个节点以及后一个节点,以便正确链接。

AC代码

class Solution {
public:
ListNode* reverseBetween(ListNode* head, int m, int n) {
if (head == NULL || m < 1 || n<1 || m >= n)
return head; int len = 0;
ListNode *p = head;
//计算链表长度
while (p)
{
len++;
p = p->next;
}
//如果要反转的范围不符合要求则返回
if (m > len || n > len)
return head; //声明一个栈,用来保存要求反转的子序列所有节点
stack<ListNode *> stk; if (m == 1)
{
//若从头节点反转,特殊处理
ListNode *r = head;
while (m <= n && r != NULL)
{
stk.push(r);
r = r->next;
++m;
}
//新的头结点
head = stk.top();
stk.pop();
//链接剩下节点
ListNode *q = head;
while (!stk.empty())
{
q->next = stk.top();
q = q->next;
stk.pop();
}
//链接尾节点
q->next = r;
return head;
}
else{
ListNode *pre = head;
//找到要反转的第一个节点的前驱并保存
int i = 1;
while (i < m - 1)
{
pre = pre->next;
++i;
} //将所有反转节点入栈,并保存尾
ListNode *r = pre->next;
while (m <= n)
{
stk.push(r);
r = r->next;
++m;
}//while //链接
while (!stk.empty())
{
pre->next = stk.top();
pre = pre->next;
stk.pop();
}
pre->next = r;
return head;
}
return head;
}
};

GitHub测试程序源码

LeetCode(92) Reverse Linked List II的更多相关文章

  1. LeetCode(206) Reverse Linked List

    题目 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed eithe ...

  2. LeetCode(92):反转链表 II

    Medium! 题目描述: 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明:1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5-&g ...

  3. LeetCode(7)Reverse Integer

    题目: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 分析: ...

  4. leetcode之旅(9)-Reverse Linked List

    题目描述: Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed ei ...

  5. LeetCode(234) Palindrome Linked List

    题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) t ...

  6. LeetCode(151) Reverse Words in a String

    题目 Given an input string, reverse the string word by word. For example, Given s = "the sky is b ...

  7. LeetCode(63):不同路径 II

    Medium! 题目描述: 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” ). 机器人每次只能向下或者向右移动一步.机器人试图达到网格的右下角(在下图中标记为“F ...

  8. LeetCode(45): 跳跃游戏 II

    Hard! 题目描述: 给定一个非负整数数组,你最初位于数组的第一个位置. 数组中的每个元素代表你在该位置可以跳跃的最大长度. 你的目标是使用最少的跳跃次数到达数组的最后一个位置. 示例: 输入: [ ...

  9. LeetCode(40):组合总和 II

    Medium! 题目描述: 给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数 ...

随机推荐

  1. 命令行媒体处理工具 FFmpeg

    FFmpeg 是一套在命令行界面运行的跨平台媒体处理工具,属于自由软件,常用来对视频音频和图片等媒体文件进行格式转换.分割和合并等,也可录屏录音. 开发语言:C官网:https://www.ffmpe ...

  2. vs2013缺少Mvc 怎么办?

    命名空间System.Web中不存在类型或命名空间名称Mvc是否缺少程序集引用? 以前vs2010或2012以前的引用一下就有了,为何现在vs2013没有,该这么处理呢?? 解决方案: 打开PCM:  ...

  3. 剑指offer部分编程题

    一 斐波那契数列 题目描述: 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项. n<=39 问题分析: 可以肯定的是这一题通过递归的方式是肯定能做出来,但是这样会有 ...

  4. html5基础知识学习

    HTML5腾讯课堂--html5前端开发视频 https://ke.qq.com/course/89671 html5_video http://www.myexception.cn/jquery/2 ...

  5. centos6.3下postgresql-9.3安装记录

    Xshell for Xmanager Enterprise 4 (Build 0186) Copyright (c) 2002-2011 NetSarang Computer, Inc. All r ...

  6. hihocoder1080 更为复杂的买卖房屋姿势

    思路: 线段树区间修改,需要使用两个懒标记set和add.处理好两个标记的优先级即可(set之前的set和add是没有作用的). 实现: #include <bits/stdc++.h> ...

  7. 降低PNG图片存储大小方法、图片压缩方法

    降低PNG图片存储大小方法,图片压缩方法,如何降低PNG图片存储大小?前提是分辨率和尺寸大小不变,图形的透明部分不变.请看如下办法,亲测可用. 1. 将PNG图片用PS打开. 2. 图像-模式-8位/ ...

  8. jmeter中文件上传配置

  9. 删除.cpp文件

    今天启动vc6.0后随手直接建了一个.cpp文件(没有建什么工程的),编译运行成功后,就把vc关了.后想把这个随手建的文件给删掉,却怎么也找不到这个文件,文件搜索或改变文件的属性也无法找到这个文件,即 ...

  10. 如何诊断 11.2 集群节点驱逐问题 (文档 ID 1674872.1)

    适用于: Oracle Database - Enterprise Edition - 版本 11.2.0.1 到 11.2.0.2 [发行版 11.2]本文档所含信息适用于所有平台 用途 这篇文档提 ...