题目:2.2.2 Reverse Linked List II

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->nullptr, m = 2 and n = 4,

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

Note: Given m, n satisfy the following condition: 1 <= m <= n <= length of list.

链表旋转是个经典问题,可以锻炼我们操作链表的能力,值得好好学习一下。

我的方法:对head和tail特殊化处理

当m<=i<=n时,通过记录前继结点prev,简单的调转next指针方向,并记录m结点pm。最后迭代到n+1时,再处理first(pm->next)和last(p)结点的指向问题。

更好的方法:一般化处理

将整个过程一般化处理的方法为:插入到1后面,第二次将4插入到1后面,即1=>2=>3=>4旋转为1=>3=>2=>4。如果能解决的话,那么继续1=>3=>2=>4=>5就能旋转为1=>4=>3=>2=>5,从而解决问题。总结:首先举最简单的例子解决,然后看解决办法能否继续推广到更复杂一些的例子,避免复杂的特殊化处理

代码实现

理解了整体设计,自己实现或读代码就很简单了。开始真正处理前要记住head(m的前一个结点)和pm(索引为m的结点),就可以开始旋转了。

Leetcode解题-链表(2.2.2)ReverseLinkedList的更多相关文章

  1. Leetcode解题-链表(2.2.0)基础类

    1 基类的作用 在开始练习LeetCode链表部分的习题之前,首先创建好一个Solution基类,其作用就是: Ø  规定好每个子Solution都要实现纯虚函数test做测试: Ø  提供了List ...

  2. Leetcode解题-链表(2.2.6)RotateList

    1 题目:Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Fo ...

  3. Leetcode解题-链表(2.2.3)PartitionList

    题目:2.2.3 Partition List Given a linked list and a value x, partition it such that all nodes less tha ...

  4. Leetcode解题-链表(2.2.1)AddTwoNumbers

    1 题目:2.2.1 Add Two Numbers You are given two linked lists representing two non-negative numbers. The ...

  5. Leetcode解题思想总结篇:双指针

    Leetcode解题思想总结篇:双指针 1概念 双指针:快慢指针. 快指针在每一步走的步长要比慢指针一步走的步长要多.快指针通常的步速是慢指针的2倍. 在循环中的指针移动通常为: faster = f ...

  6. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  7. 【算法题 14 LeetCode 147 链表的插入排序】

    算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # ...

  8. LeetCode 单链表专题 (一)

    目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...

  9. leetcode解题报告(2):Remove Duplicates from Sorted ArrayII

    描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

随机推荐

  1. [LeetCode] Closest Leaf in a Binary Tree 二叉树中最近的叶结点

    Given a binary tree where every node has a unique value, and a target key k, find the value of the n ...

  2. [LeetCode] Flood Fill 洪水填充

    An image is represented by a 2-D array of integers, each integer representing the pixel value of the ...

  3. [LeetCode] Task Scheduler 任务行程表

    Given a char array representing tasks CPU need to do. It contains capital letters A to Z where diffe ...

  4. [LeetCode] Can Place Flowers 可以放置花

    Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, ...

  5. ●杜教筛入门(BZOJ 3944 Sum)

    入门杜教筛啦. http://blog.csdn.net/skywalkert/article/details/50500009(好文!) 可以在$O(N^{\frac{2}{3}})或O(N^{\f ...

  6. [LSGDOJ 1505]售货员的难题 状压DP

    题目描述 某 乡有n个村庄(1<n<15),有一个售货员,他要到各个村庄去售货,各村庄之间的路程s(0<s<1000)是已知的,且A村 到B村与B村到A村的路大多不同.为了提高 ...

  7. ●BZOJ 1969 [Ahoi2005]LANE 航线规划

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=1969 题解: 线段树,树链剖分,反向考虑思路是很巧妙,但是感觉代码真的恶心.. 反着考虑,先 ...

  8. VK-Cup 2017 qualification 1

    VK-Cup,cf里面只有切成俄文才能看到,题目也都是俄文的(百度翻译成英文和中文). 两人组队参赛的,赛期1天,乐多赛赛制(和时间基本无关,交上去挂了扣分).这次是第一场资格赛. 这次又和ditol ...

  9. [BZOJ]3110 K大数查询(ZJOI2013)

    这大概是唯一一道小C重写了4次的题目. 姿势不对的树套树(Fail) → 分块(Fail) → 整体二分(Succeed) → 树套树(Succeed). 让小C写点心得静静. Description ...

  10. Atom 编辑器安装 linter-eslint 插件,并配置使其支持 vue 文件中的 js 格式校验

    安装方式有如下几种. 1.最常用的安装方式. # 进入atom插件文件夹 cd ~/.atom/packages/ # git clone 插件源文件 git clone https://github ...