How to quickly merge two linked lists

The question of how to quickly merge two linked lists was encountered during the interview. The interviewer was Mr. Lei Zhou, a senior CTO of Microsoft. During the interview, Mr. Zhou asked a total of three questions:

1. Combine two unordered linked lists into one, taking into account the least time and memory (time complexity and space complexity in the data structure):

When I got this question, I thought at first to sort the two linked lists, and then insert them into a new linked list. Although this effect can be achieved, it is obviously not the optimal solution, and then I thought that since the considered memory is the best , that can be modified on the original basis. My idea is as follows: first sort the two sets, then fix one set for traversal, and insert the elements in the other set into it. This method also seems to be able to reach The expected effect, at this time, Mr. Zhou shook his head and said that it still doesn't work. Your thinking is fixed on sorting. Sorting is a waste of time. Why not change your thinking? Time and memory to consider, you can actually do this, create an empty linked list , insert the other two into this table (I thought this was not the same as the first idea at the time), before I finish, the essence is to compare the first element of the two linked lists each time to get the smaller one (if the To the big sort) insert into the new linked list in order, and delete the inserted elements, so as to ensure that only the first element is compared each time, and the outer layer uses a while loop control, which is much more efficient than the previous one

2. Test a thinking logic question: There are 100 radishes in a pile, and a rabbit can carry up to 50 radishes at a time. This rabbit carries radishes home. Her home is 50 meters away from the pile of radishes, but this rabbit loves it too much. After eating radishes, he has to eat one at every step. How many radishes can the rabbit carry at most? (The most important thing to do is to eat)

3. I don’t remember this question very clearly. After all, it was an interview question 2 years ago. How many rabbits are left at most? Looking forward to your comments from God

In the end, I found that as long as the interviewers are technical experts, they will ask some very basic and low-level things. It happens that we are easy to ignore. They value our thinking logic and research spirit more.

Guess you like

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