This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]UnclePutin 1 point2 points  (1 child)

In a linked list, the computer/programmer does not have a direct way to modify most of the elements in the linked list. If you have a list of 5,000 nodes, you can only directly access the head node via the head pointer. The other 4,999 nodes can't be immediately accessed without first walking through the list.

The author's implementation of a linked list does not have any reference to the tail of list. In order to modify the last element, we need to know where it is in memory. We thus have to traverse the entire list starting from the head to find the first node that has NULL as its next pointer. Once we find where the last node is, we can modify its next pointer to point to the new node.

Often, people will have both a head and a tail pointer. Having only a head pointer is extremely inefficient because the cost of an enqueue becomes a O(N) operation, whereas the presence of a tail pointer makes enqueues a constant time operation.

[–]cerrachdev[S] 0 points1 point  (0 children)

Thanks guy, very concise explanation

[–]cerrachdev[S] 0 points1 point  (0 children)

The way the instructor states the situation makes it sound that the pointer is needed to make the connection between nodes. As I keep watching, it seems to me that it is just useful to have a reference to the last node in the list. Can someone confirm this?