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) ...
随机推荐
- Node.js做的代理转发服务器
可以代理苹果ID服务器 const http = require('http'); const https = require('https'); const client = require('ht ...
- [UE4]获得特定类型的所有Actor:Get All Actors Of Class、Get All Actors with Interface、Get All Actors with Tag
- Java给整数部分的字符串加上千分位分隔符
本来想网上找个例子,结果让人很失望,网上的大部分用的DecimalFormat .NumberFormat,我随便搞了一个长点的字符串,发现大部分都有小数进度问题. 而且网上的人,都不测试的,写的例子 ...
- 2017湖湘杯复赛writeup
2017湖湘杯复赛writeup 队伍名:China H.L.B 队伍同时在打 X-NUCA 和 湖湘杯的比赛,再加上周末周末周末啊,陪女朋友逛街吃饭看电影啊.所以精力有点分散,做出来部分题目,现在 ...
- servlet的执行过程简介(从tomcat服务器和web应用的角度)
该链接详解htttp请求和响应 http://www.cnblogs.com/goxcheer/p/8424175.html 1.web应用工程发布到tomcat服务器 2.客户端访问某个web资源, ...
- 封装MemoryCache
一.定义一个缓存接口IChace using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- 表单(同步提交)和AJAX(异步提交)示范
表单提交(同步提交) HTML文件: PHP文件: 这样就能接收到HTML里输入的内容,注意: FORM表头method为POST,PHP文件获取的方法就是$_POST,method为GET,PHP的 ...
- jquery事件及插件
jquery事件 方法 描述 bind() 向匹配元素附加一个或更多事件处理器 blur() 触发.或将函数绑定到指定元素的 blur 事件 change() 触发.或将函数绑定到指定元素的 chan ...
- js保留两位小数点
var temp = 2.222222222; temp.toFixed(2); //得出结果2.22
- MySQL免安装配置(亲测过,请放心借鉴)
下载地址:https://dev.mysql.com/downloads/mysql 1.mysqld --initialize-insecure 初始化 2.mysqld install 安装服 ...