简体   繁体   中英

Running mergesort on two linkedLists that are coupled

I have the following problem and would like to see if somebody could tell me if I'm on the right track with my approach:

I want to do a "rolling window" kind of computation on two linked lists and for that I need them to be sorted by magnitude. If I just have one linked list, writing the corresponding mergesort is not the problem. However, now I'm wondering how I should go about the fact that I have two linked lists where I want to have the corresponding elements from list 1 and list 2 move together as I sort by the magnitude of list 1. If this is not entirely clear, this is what I mean:

In list 1, I want to do a sort by magnitude, so basically just rearrange the pointers. Whenever I move element "n" in one list, however, I also need to move the corresponding element "n" in the other list to the same position as the element from the other one.

Would my approach of using mergesort for this be the right way to go or does anyone know a better approach? I am having a hard time imagining how I would go about reordering the second list while mergesorting the first one since the second list is not necessarily going to be sorted by magnitude anymore and I need the individual elements to correspond to each other. Thanks!

Marc

只需创建一对对应元素的列表,然后按该对的第一个元素对列表进行排序即可。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM