题目

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. 如何解决netty发送消息截断问题

      在netty开发过程中我遇到过长的消息被分割成多个小消息的问题.如下图所示: 其实这两条消息应该是一条消息,它们两个才是一个完整的json字符串.查看代码原来是客户端与服务器端都没有考虑TCP粘包 ...

  2. Day2课后作业:sed替换程序

    #!/usr/bin/env python #_*_conding:utf-8_*_ import sys,os old_file = sys.argv[1] new_file = sys.argv[ ...

  3. Qt 2D绘图之六:图形视图框架的事件处理与传播

    一.简介 图形视图框架中的事件都是首先由视图进行接收,然后传递给场景,再由场景传递给相应的图形项.而对于键盘事件,它会传递给获得焦点的图形项,可以使用QGraphicsScene类的setFocusI ...

  4. [JLOI2016]圆的异或并

    Description 在平面直角坐标系中给定N个圆.已知这些圆两两没有交点,即两圆的关系只存在相离和包含.求这些圆的异或面积并.异或面积并为:当一片区域在奇数个圆内则计算其面积,当一片区域在偶数个圆 ...

  5. CSS以及JS各种库的在线CDN引用地址

    JS类—— html5.js,让你的IE浏览器支持H5新特性:http://html5shiv.googlecode.com/svn/trunk/html5.js   (记得要注释判断哦) JQuer ...

  6. Springboot日志配置探索(主要看logback)(二)

    这篇博客主要是讲在Springboot中扩展的日志框架的配置,也是主要讲logback 8 继续看文档,这里讲到: springboot里面还有几个日志系统框架可以选择使用,你可以通过在classpa ...

  7. 定时任务-ScheduledExecutorService

    创建定时任务线程池的方式 ScheduledExecutorService executorService = Executors.newScheduledThreadPool(4);// 不推荐// ...

  8. linux下mysql中文乱码

    登录mysql执行mysql> show variables like 'character%';发现编码有些不是utf-8 修改/etc/mysql/my.cnf,网上说的是/etc/my.c ...

  9. (转)UVM挑战及概述

    UVM的调度也具有其独特的挑战,尤其是在调试的领域.其中的一些挑战如下: 1. Phase的管理:objections and synchronization 2. 线程调试 3. Tracing i ...

  10. log explorer使用的几个问题[转载]

    1)对数据库做了完全 差异 和日志备份备份时选用了删除事务日志中不活动的条目再用Log explorer打试图看日志时提示No log recorders found that match the f ...