Leetcode 题解 reverse List II
这个题确实太容易错了。
我已经做了2遍了,之前都是套用reverse List 1中的函数。
现在尝试用新方法,在一个函数里完成,结果又错了。
事实证明,永远不要想当然!!!白板编程真的是要求,你对每一行代码都知道在做什么!尤其是边界条件。
因为没有编译调试环境,写错了,你根本看不出来,没有修改的机会啊!!
要求一遍就过啊!
这太难了。看看我提交的这些题目,几个是一遍就过的???都是写完先跑一遍再说,有错再慢慢改!!
毛病啊!!
struct ListNode* reverseBetween(struct ListNode* head, int m, int n) {
if( m >= n) return head;
struct ListNode * dummy = (struct ListNode *)malloc(sizeof(struct ListNode));
dummy->next = head;
struct ListNode* pm, *pn,*pre,*last,*mid;
int i;
pm = dummy;
for(i = ; i < m-;i++) //找到m前边那个 i表示当前pm指向的是哪个。初始pm指向第0个,最后指向第m-1个。出口(i = m-1)
{
pm = pm->next;
}
pn = dummy;
for(i = ; i < n; i ++) //初始指向第0个,最后指向第n个
{
pn = pn ->next;
}
mid = pm->next;
last = pn->next;
// 这里太他妈容易错了!!! while(mid != pn->next) 因为当mid = pn的循环,pn->next的值就被改变了!
struct ListNode *end = pn->next;
while(mid != end)
{
pre = mid->next;
mid->next = last;
last = mid;
mid = pre;
}
pm->next = last;
mid = dummy->next;
free(dummy);
return mid;
}
Leetcode 题解 reverse List II的更多相关文章
- [LeetCode 题解]: Reverse Nodes in K-Groups
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a li ...
- leetCode题解 Reverse Words in a String III
1.题目描述 Given a string, you need to reverse the order of characters in each word within a sentence wh ...
- LeetCode 541. Reverse String II (反转字符串 II)
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
- LeetCode题解——Reverse Integer
题目: 数字翻转,即输入123,返回321:输入-123,返回-321. 代码: class Solution { public: int reverse(int x) { , sign = ; ) ...
- leetcode题解||Reverse Integer 问题
problem: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 ...
- [LeetCode 题解]: Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- LeetCode题解-----Majority Element II 摩尔投票法
题目描述: Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The a ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- leetcode 344. Reverse String 、541. Reverse String II 、796. Rotate String
344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ...
随机推荐
- html标签SEO规范
原文地址:http://blog.sina.com.cn/s/blog_6c3898dd0100whr7.html 1.<!--页面注解--> 2.<html> 3.<h ...
- webview之总结2
21,js与androud交互之javascript调用本地之方法一(接口类): ========= 21,js与androud交互之javascript调用本地之方法一(接口类): Android4 ...
- [UE4]裁剪 Clipping
Clipping裁剪,是每个UI都有的属性.一般是在容器UI上设置,对容器内的UI进行裁剪. 一.Clip to Bounds:裁剪到边界 二.Clip To Bounds - Without Int ...
- [UE4]场景加载界面
就可以这样就可以了,当另外一个场景成功打开后,场景加载界面也会自动消失(因为这是加载界面是添加到当前场景). 加上delay是为了在小场景测试的时候可以方便看到加载场景,避免场景加载过快看不到,不加的 ...
- [UE4]运行时进入观察模式
- 读《SQL优化核心思想》:你不知道的优化技巧
SQL性能问题已经逐步发展成为数据库性能的首要问题,80%的数据库性能问题都是因SQL而导致. 1.1 基数(CARDINALITY) 某个列唯一键(Distinct_Keys)的数量叫作基数.比如性 ...
- Android Studio快捷键Ctrl+Shift+F不能用,全局搜索不能用;
AS全局搜索Ctrl+Shift+F突然就不能用了,在AS找半天没有找到问题,原因竟然是和搜狗输入法的简繁切换冲突了:下面有图把简繁切换关闭或更换快捷键后,as的全局搜索就能用了:
- vuejs实现瀑布流布局(三)
前面写过vuejs实现的瀑布流布局,<vuejs实现瀑布流布局(一)>和<vuejs实现瀑布流布局(二)>也确实实现了瀑布流布局,但是这个是基于SUI-Mobile实现的无限滚 ...
- 零基础学习python_字符串(14-15课)
今天回顾下我之前学习python的第一个对象——字符串,这个对象真蛋疼,因为方法是最多的,也是最常见的类型,没有之一... 内容有点多,我就搜了下网上的资料,转载下这个看起来还不错的网址吧:http: ...
- 【Git使用】SourceTree+Git简单使用(Windows)(转)
导读: 本人过去Git的可视化工具用的是TortoiseGit,虽然Android Studio也能进行版本管理,但是用下来,感觉SoureTree这款工具是最舒服的(免费的),下面就给大家介绍下我的 ...