I want to know that do “head” and “tail” store any value like the other nodes??
thanks
I want to know that do head and tail store any value like the
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Depends on the specific implementation of the linked list. Both ways can be made to work, but having extra “head/tail” elements greatly simplifies the implementation of many operations like insert/remove (since you can always be sure there will be a next/previous element, saving a lot of conditional code to deal with the start/end of the list).
Since you tagged your question as Java, i suggest you take a look at the JDK source code of java.util.LinkedList. You will find that in this specific implementation head and tail do not refer to an element.
If you really want to understand the advantage of extra head/tail nodes its probably most instructive if you implement a simple double linked list by yourself to experience the difference in complexity for the insert/remove methods firsthand.
In other languanges that allow pointer arithmetic, there is often only an extra head node which also serves as the tail node at the same time.