Comments (3)
Delete cur 后,cur 所指向的对象释放了,但cur只是个名字,这个名字还是可以复用的
On Tuesday, August 12, 2014, Lice Pan [email protected] wrote:
P44页讲到了Remove Duplicates from Sorted List的迭代解法,其中的一段代码片段:
for (ListNode *prev = head, *cur = head->next; cur; cur = cur->next) {
if (prev->val == cur->val) {
prev->next = cur->next;
delete cur;
} else {
prev = cur;
}}既然delete cur语句已经将cur删除,为什么还可以在for的条件语句里执行cur = cur->next?
PS.感谢作者辛勤的劳动为我们带来了如此有价值的作品,再次感谢:)
—
Reply to this email directly or view it on GitHub
#27.
My tech blog: http://www.soulmachine.me
My GitHub: https://github.com/soulmachine
My LinkedIn: http://www.linkedin.com/in/soulmachine/
My Sina Weibo: http://weibo.com/soulmachine
from leetcode.
delete cur之后cur->next还能引用?本人c++菜鸟,望指教:)
from leetcode.
这个问题我有看法,delete cur之后,cur当然那还能被附新值,但是继续使用cur->next 赋值且程序通过的原因是cur原来指向的内存没有被其他程序污染,所以可以找到cur->next,其实这样写是不太妥当的,我改成了下面这个写法,功能一样,就是防止面试官质疑:
class Solution {
public:
ListNode *deleteDuplicates(ListNode *head) {
if(head == NULL) return NULL;
for(ListNode *prev = head, *curr = head->next;
curr;
curr = curr->next){
if(prev->val == curr->val){
ListNode *next = curr->next;
prev->next = next;
delete curr;
curr = prev;
}
else{
prev = curr;
}
}
return head;
}
};
from leetcode.
Related Issues (20)
- 这个有源码下载吗
- MWE for Codex HOT 1
- 暴力枚举Combinations代码添加cur和k比较条件,可以减少遍历次数
- 闲鱼有人在售卖本书牟利 HOT 1
- 请问有java版本嘛,没有找到java版本的 HOT 1
- qq群已满员。。。请问有2群吗,有好心人拉我进去吗 qq:26799713968
- 希望能出python版本
- 请问有Java版的pdf吗 HOT 2
- Is there a traditional Chinese version? HOT 1
- NumTrees 一题的解法似乎有改进余地
- Regular Expression Matching
- Merge k Sorted Lists
- Symmetric Tree递归算法
- English version of book
- LRU Cache: Not necessary to update cacheMap after splice
- 字体链接失效 HOT 1
- Leetcode Java Sqrt(x) 题代码错误 HOT 1
- 15.1 为啥要用long long?
- 2.1.18 第30页勘误
- Python version
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leetcode.