题目

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. laravel配合swoole使用总结

    最近对接硬件做了两个项目,用到了swoole 第一个是门禁系统,需要远程开门.离线报警.定时开门.离线刷卡等功能 1.远程开门: 目前用cli创建个临时客户端连接服务端发送命令,服务端处理完成后客户端 ...

  2. Codeforces Round #390 (Div. 2) B

    Ilya is an experienced player in tic-tac-toe on the 4 × 4 field. He always starts and plays with Xs. ...

  3. css hack 笔记

    body{background-color:#000\9;}/*ie*/ body{background-color:#0f0\9\0;}/*ie9及以上*/ body{background-colo ...

  4. PS基础知识学习

    PS学习视频(全) https://ke.qq.com/webcourse/index.html#course_id=28554&term_id=100014572&taid=1349 ...

  5. [牛客网试题] Test.main() 函数执行后的输出是()

    public class Test { public static void main(String [] args){ System.out.println(new B().getValue()); ...

  6. 2019/05/11 JAVA虚拟机原理

    所谓虚拟机,就是一台虚拟的机器.他是一款软件,用来执行一系列虚拟计算指令,大体上虚拟机可以分为 系统虚拟机和程序虚拟机, 大名鼎鼎的Visual Box.Vmare就属于系统虚拟机,他们完全是对物理计 ...

  7. AJPFX关于VIM的常用快捷键

    Ajax技术的核心是XMLHttpRequest对象(简称XHR),var xhr = new XMLHttpRequest();function createXHR(){if (typeof XML ...

  8. 3、从尾到头打印链表------------>剑指offer系列

    题目 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 分析 要了解链表的数据结构: val属性存储当前的值,next属性存储下一个节点的引用. 要遍历链表就是不断找到当前节点的nex ...

  9. Git 学习教程【转+总结】

    之前是在用SVN,现在因为小伙伴比较喜欢Git,所以也开始学习Git,很感谢 时光穿梭机 - 廖雪峰 的无私奉献.本文用来记录我在学习Git过程中的收获和笔记,廖雪峰大神的Git教程参考这里. 1.G ...

  10. 【Web应用】JAVA网络上传大文件报500错误

    问题描述 当通过 JAVA 网站上传大文件,会报 500 错误. 问题分析 因为 Azure 的 Java 网站都是基于 IIS 转发的,所以我们需要关注 IIS 的文件上传限制以及 requestT ...