when to use a dummy node in linked list questions?

tags: learning leetcode programming

content

If there’s any chance the head changes — use a dummy node.

ChatGPT

  • for the question “remove node from linked list”, the head doesn’t change. why is a dummy node still used?
    • wrong! the head might change in this question
    • if we have [1 -> 2 -> 3] and we need to remove the 3rd node from the end, it’s actually the head that’s to be removed
    • hence the head changes! we need dummy node!

up

down

reference