Leetcode解题-链表(2.2.2)ReverseLinkedList
题目: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的更多相关文章
- Leetcode解题-链表(2.2.0)基础类
1 基类的作用 在开始练习LeetCode链表部分的习题之前,首先创建好一个Solution基类,其作用就是: Ø 规定好每个子Solution都要实现纯虚函数test做测试: Ø 提供了List ...
- 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 ...
- 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 ...
- Leetcode解题-链表(2.2.1)AddTwoNumbers
1 题目:2.2.1 Add Two Numbers You are given two linked lists representing two non-negative numbers. The ...
- Leetcode解题思想总结篇:双指针
Leetcode解题思想总结篇:双指针 1概念 双指针:快慢指针. 快指针在每一步走的步长要比慢指针一步走的步长要多.快指针通常的步速是慢指针的2倍. 在循环中的指针移动通常为: faster = f ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- 【算法题 14 LeetCode 147 链表的插入排序】
算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # ...
- LeetCode 单链表专题 (一)
目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...
- leetcode解题报告(2):Remove Duplicates from Sorted ArrayII
描述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
随机推荐
- NC帮助文档网址
NC帮助文档: https://wenku.baidu.com/view/2d05a77c0b4e767f5acfceb6.html NC方法总结: ...
- Python 字符串常见的27个操作
有字符串 mystr = "hello world itcast and itcastcpp",以下是常见的操作: 1. mystr.find(str, start=0, end= ...
- 最快的3x3中值模糊
10.1国庆后,知名博主:laviewpbt http://www.cnblogs.com/Imageshop/ 发起了一个优化3x3中值模糊的小活动. 俺也参加其中,今天博主laviewpbt ...
- [Codeforces 919F]A Game With Numbers
Description 题库链接 两个人 Van♂ 游戏,每人手上各有 \(8\) 张牌,牌上数字均为 \([0,4]\) 之间的数.每个人在自己的回合选自己手牌中数字不为 \(0\) 的一张与对方手 ...
- [ZJOI 2006]超级麻将
Description Input 第一行一个整数N(N<=100),表示玩了N次超级麻将. 接下来N行,每行100个数a1..a100,描述每次玩牌手中各种牌的数量.ai表示数字为i的牌有ai ...
- 计蒜客NOIP2017提高组模拟赛(五)day1-展览
传送门 发现这题选或不选对状态的优劣程度不会产生影响,如果已经确定了两个数a和b,那么最优的首项和公比也都是唯一确定的, 与对于后面的数x,加进去也好不加进去也好,首项和公比依旧是原来的 于是我们用尺 ...
- 【USACO17JAN】Promotion Counting晋升者计数 线段树+离散化
题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...
- HDU2108和HDU2036(叉乘)
hdu2108 判断是否为凸边形 判断连续三点的叉乘 若为凸,内角<180:若为凹,内角>180 所以通过正负来判断 #include <iostream> #include ...
- hdu 5112 (2014北京 水)
题意:有个人在跑步,一直每个时间他所在的位置,求最大速度 #include <iostream> #include <cstring> #include <cstdio& ...
- [BZOJ]1019 汉诺塔(SHOI2008)
找规律成功次数++. Description 汉诺塔由三根柱子(分别用A B C表示)和n个大小互不相同的空心盘子组成.一开始n个盘子都摞在柱子A上,大的在下面,小的在上面,形成了一个塔状的锥形体. ...