Skip to content

LeetCode题解c++版本的P44页有问题 #27

Open
@aCayF

Description

@aCayF

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.感谢作者辛勤的劳动为我们带来了如此有价值的作品,再次感谢:)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions