What is the 'head' of a linked list?

后端 未结 4 758
野的像风
野的像风 2020-12-24 03:15

I\'m working in linked lists in Java, so I\'m trying to grasp the concept of a single linked list.

head -> 12 -> 34 -> 56 -> null

4条回答
  •  情书的邮戳
    2020-12-24 03:58

    The term “head” has two completely unrelated meanings. The most common (that comes out of Lisp, I believe) is “the first element of the list.” Judging from your diagram, that is not the meaning you have in mind.

    The second meaning refers to a technique to deal with the following issue: If you represent a linked list as just the nodes with data, then when the list is empty, all references (and/or pointers, depending on the language) to the list have to be null, because there is nothing to point at. This creates lots of bookkeeping problems for code that uses the list. A list head solves this problem. It is a list node that contains no actual data. A reference or pointer to the list is always a pointer to the head node. The first element of the list is always head.next. Usually the existence of the head is hidden in the class that implements a “linked list with head.”

    Depending on the operations being supported, there can be a similar bookkeeping problem at the end of the list, particularly for doubly linked lists. A list tail node simplifies bookkeeping.

    These are also called “sentinel nodes” in the literature (including the Wikipedia article on linked lists).

提交回复
热议问题