GithubHelp home page GithubHelp logo

Comments (3)

soulmachine avatar soulmachine commented on May 13, 2024

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.

aCayF avatar aCayF commented on May 13, 2024

delete cur之后cur->next还能引用?本人c++菜鸟,望指教:)

from leetcode.

riveridea avatar riveridea commented on May 13, 2024

这个问题我有看法,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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.