Talking about the difference between singly linked list and double linked list

During the interview yesterday, the interviewer asked me a question about linked lists: the situation is as follows

Interviewer: Can you tell me the difference between a linked list and an array?

Me: Arrays allocate memory statically, and linked lists dynamically allocate memory; arrays are continuous in memory, but linked lists are discontinuous; array elements are in the stack area, and linked list elements are in the heap area; arrays are located using subscripts, and the time complexity is O(1). The time complexity of positioning an element is O(n); the time complexity of inserting or deleting elements in an array is O(n), and the time complexity of a linked list is O(1).

Interviewer: Then tell me the difference between a singly linked list and a double linked list?

I:

A singly linked list has only one pointer to the next node, that is, only next
In addition to a pointer to the next node, a doubly linked list also has a pointer to the previous node. You can quickly find the previous node through prev(). As the name suggests, a singly linked list can only be read in one direction.

Interviewer: From your description, the double-linked list can be searched and deleted by using the idea of ​​dichotomy, so the efficiency will be greatly improved, but why is the application of the singly-linked list in the current market more than the double-linked list? Is the application more extensive?

Me: ...I really don't know about this, and then the interviewer reminded me to consider the problem from the perspective of storage efficiency...

After I came back, I went to Baidu and found that most of the answers on the Internet were about the code implementation of the linked list, and there was no in-depth analysis of the essence of the linked list, so I did the following analysis:

The structure diagram of singly linked list and double linked list is as follows:

From the above structure, it can be concluded that the doubly linked list has the following advantages:

1. When deleting a node in a singly linked list, you must get the precursor of the node to be deleted. There are two ways to get the precursor. The first method is to locate the node to be deleted and save the current node all the way. precursor. The second method is to locate the predecessor from the beginning of the singly linked list again after locating the node to be deleted. Although method one is usually used. But in fact, the efficiency of these two methods is the same, and the total movement operation of the pointer will be 2*i times. And if you use a doubly linked list, you don't need to locate the predecessor node. Therefore, the total movement of the pointer is i times.

2. The same is true when searching. We can use the idea of ​​dichotomy to search from the middle node at the same time, so that the efficiency of the double-linked list can be doubled.

But why do single-linked lists on the market use redundant double-linked lists?

From the storage structure point of view, each double-linked list node has one more pointer than a single-linked list node, and a length of n requires n*length (this length is 4 bytes in a 32-bit system) space, which is in some cases. The pursuit of time efficiency is not suitable for applications, because it takes up more space than a singly linked list; at this time, the designer will adopt the practice of replacing space with time, which is a measure of the overall project.





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325645602&siteId=291194637