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 ...
随机推荐
- vue-cli 去掉严格模式:
vue-cli 去掉严格模式: 在根目录中找到 .eslintignore 文件,添加 严格模式需要监听的目录 src/*
- [LeetCode] Pour Water 倒水
We are given an elevation map, heights[i] representing the height of the terrain at that index. The ...
- CLR-2-2-引用类型和值类型
引用类型和值类型,是一个老生常谈的问题了.装箱拆箱相信也是猿猿都知,但是还是跟着CLR via C#加深下印象,看有没有什么更加根本和以前被忽略的知识点. 引用类型: 引用类型有哪些这里不过多赘述,来 ...
- JS点击按钮打开新的独立页面
工作中遇到需要点击按钮弹出一个独立的页面,并显示指定内容的问题,查了一些资料后,得到以下方法: window.open('locationPage.html', '_blank', 'height=1 ...
- hibernate--CRUD初体验
hibernate的crud操作初体验. 看具体实例 package com.fuwh.model; import javax.persistence.Column; import javax.per ...
- [NOI2009]变换序列
Description Input Output Sample Input 5 1 1 2 2 1 Sample Output 1 2 4 0 3 HINT 30%的数据中N≤50: 60%的数据中N ...
- [HNOI2009]无归岛
Description Neverland是个神奇的地方,它由一些岛屿环形排列组成,每个岛上都生活着之中与众不同的物种.但是这些物种都有一个共同的生活习性:对于同一个岛上的任意两个生物,他们有且仅有一 ...
- [Ahoi2005]LANE 航线规划
题目描述 对Samuel星球的探险已经取得了非常巨大的成就,于是科学家们将目光投向了Samuel星球所在的星系——一个巨大的由千百万星球构成的Samuel星系. 星际空间站的Samuel II巨型计算 ...
- UVALive - 3530:Martian Mining
dp 可以发现,对于(i,j),要么把它运上去,那么把它运到左边,枚举一下即可 #include<cstdio> #include<cstdlib> #include<a ...
- ●POJ 1556 The Doors(简单计算几何+最短路)
●赘述题目 10*10的房间内,有竖着的一些墙(不超过18个).问从点(0,5)到(10,5)的最短路. 按照输入样例,输入的连续5个数,x,y1,y2,y3,y4,表示(x,0--y1),(x,y2 ...