题目

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. linux增加系统监视器的快捷键

    系统命令:gnome-system-monitor 可在终端输入调用 在系统相应的快捷键设置区设置即可

  2. PL/SQL 多表关联UPDATE

    假设有两个表A和B,A表字段a,b,c,d,B表字段b,e,f,两表的关联条件是字段b,现在想做个data patch,欲将B表中的字段e的值patch给A表的字段c. 有如下两种方法: 1 upda ...

  3. P3375 【模板】KMP字符串匹配(全程注释,简单易懂)

    题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在s1中所有出现的位置. 为了减少骗分的情况,接下来还要输出子串的前缀数组next.如果你不知道这是什么意思也不要问,去百度搜[ ...

  4. ES-windos环境搭建(1)

    前言 由于elasticsearch为Java开发,所以它还依赖Java JDK环境,并且对版本还有要求,需要1.8(含)以上.我们首先来配置Java JDK环境. JDK简介 JDK是Java语言的 ...

  5. HTML5+CSS3新增内容总结!!!!!绝对干货

    说到H5C3会不会觉得东西好多啊,今天就整理了一份总结性的内容: CSS3选择器有哪些?答:属性选择器.伪类选择器.伪元素选择器.CSS3新特性有哪些?答:1.颜色:新增RGBA,HSLA模式 文字阴 ...

  6. CSS元素隐藏的display和visibility

    一.CSS元素隐藏 在CSS中,让元素隐藏(指屏幕范围内肉眼不可见)的方法很多,有的占据空间,有的不占据空间:有的可以响应点击,有的不能响应点击. { display: none; /* 不占据空间, ...

  7. Android View 背景选择器编写技巧

    在项目中选择器的使用是非常多的,以下是本人在项目中的一些常用的背景选择器的写法 带边框下划线背景选择器效果图: 上面布局中放了10个CheckBox,然后设置了CheckBox的背景图片位,背景选择器 ...

  8. PL/SQL学习笔记(四)之——删除重复记录

    例:假设员工表中有若干记录重复,请删除重复的记录(某企业面试题) ------模拟建表 create table employee( e_id varchar2(20) primary key, e_ ...

  9. C# 文件操作的工具类

    using System.IO; namespace 文件操作类 { public class FileHelper { /// <summary> /// 判断文件是否存在 /// &l ...

  10. Bootstrap历练实例:表单控件状态(禁用的字段集fieldset)

    禁用的字段集 fieldset 对 <fieldset> 添加 disabled 属性来禁用 <fieldset> 内的所有控件. <!DOCTYPE html>& ...