Should I assume there is a header node in the singly linked list?

  • 0

    I am wondering for the singly linked list, should I assume there is a header node? Or if I was given an interview about this question, should I confirm that with interviewer?

    Because in doubly linked list, I see a more popular implementation will have a header node and a tail node, which do not contain data inside, but just to make code easier to develop.

  • 2

    When in doubt, always confirm. But specifically for the problem for which you have posted this question, its solution does not require an empty header node.

  • 0

    Yes, if a extra header node is used, submitting error will be reported. But why no header node with the standard solution? There is a header node for singly linked list by default actually.

  • 0

    I also found there is a empty header node in a book, but the OJ seems no need to header node. So, maybe in actually programming(working and studying), it is better to add a header node.:D

  • 0

    maybe you can add these line in your code:

    LinkNode *realhead=new LinkNode(0);

    (don't forget to delete it after use...though OJ won't report error


    then you can operate the linkedlist as usual!

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.