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) ...
随机推荐
- 学习笔记之Git / Gitflow / TortoiseGit
Git - Wikipedia https://en.wikipedia.org/wiki/Git Git (/ɡɪt/) is a version control system for tracki ...
- kafka 生产消费原理详解
Kafka日志及Topic数据清理 https://blog.csdn.net/qiaqia609/article/details/78899298 Kafka--Consumer消费者 pastin ...
- U3D学习11——nav导航和动画
一.nav导航 1.nav mesh agent 2.off mesh link 3.navigation面板-areas标签的应用,导航分层 二.动画 1.avatar可重用. 2.Animator ...
- 命令提示符没办法登录MySQL
前几天在CMD命令下输入 MYSQL -UROOT -P 一直显示命令无效. 后来想知道原来是要在MYSQL的BIN(安装)目录下才能访问该命令,如果要在其他路径下使用sql命令,那么要配置MYSQ ...
- Hadoop Api 基本操作
hadoop环境配置好后,直接可以在window上进行调试.话不多说,直接上源码. package cn.terry; import java.io.FileInputStream; import ...
- gulp 编译es6 react 教程 案例 配置
1.gulp基本配置: var gulp = require('gulp'), watch = require('gulp-watch'), babel = require('gulp-babel') ...
- HBase数据模型和读写原理
Hbase的数据模型和读写原理: HBase是一个开源可伸缩的分布式数据库,他根据Google Bigtable数据模型构建在hadoop的hdfs存储系统之上. HBase是一个稀疏.多维度 ...
- Java 10 - Java Character类
Java Character类 使用字符时,我们通常使用的是内置数据类型char. 实例 char ch = 'a'; // Unicode for uppercase Greek omega cha ...
- python 之 json 与pickle 模块
序例化:将对象转换为可通过网络传输或可以存储到本地磁盘的数据格式(如:XML.JSON或特定格式的字节串)的过程称为序列化:反之,则称为反序列化. 1.[JSON] import json dic={ ...
- asp.net 中日期的格式化显示的方法
在Asp.net 中经常使用日期,在不同的场合,对日期的显示方式有不同的要求,为此,自己总结了一些日期格式化的方式,仅供学习参考使用: C#格式化日期时间 DateTime dt = DateTime ...