<small id='YMwam'></small><noframes id='YMwam'>

      <tfoot id='YMwam'></tfoot>

        <bdo id='YMwam'></bdo><ul id='YMwam'></ul>

      <legend id='YMwam'><style id='YMwam'><dir id='YMwam'><q id='YMwam'></q></dir></style></legend>
    1. <i id='YMwam'><tr id='YMwam'><dt id='YMwam'><q id='YMwam'><span id='YMwam'><b id='YMwam'><form id='YMwam'><ins id='YMwam'></ins><ul id='YMwam'></ul><sub id='YMwam'></sub></form><legend id='YMwam'></legend><bdo id='YMwam'><pre id='YMwam'><center id='YMwam'></center></pre></bdo></b><th id='YMwam'></th></span></q></dt></tr></i><div id='YMwam'><tfoot id='YMwam'></tfoot><dl id='YMwam'><fieldset id='YMwam'></fieldset></dl></div>

      如何在恒定空间中对单链表进行排序?

      时间:2023-10-18
    2. <legend id='l0S4S'><style id='l0S4S'><dir id='l0S4S'><q id='l0S4S'></q></dir></style></legend>

          • <bdo id='l0S4S'></bdo><ul id='l0S4S'></ul>

              <i id='l0S4S'><tr id='l0S4S'><dt id='l0S4S'><q id='l0S4S'><span id='l0S4S'><b id='l0S4S'><form id='l0S4S'><ins id='l0S4S'></ins><ul id='l0S4S'></ul><sub id='l0S4S'></sub></form><legend id='l0S4S'></legend><bdo id='l0S4S'><pre id='l0S4S'><center id='l0S4S'></center></pre></bdo></b><th id='l0S4S'></th></span></q></dt></tr></i><div id='l0S4S'><tfoot id='l0S4S'></tfoot><dl id='l0S4S'><fieldset id='l0S4S'></fieldset></dl></div>
            • <tfoot id='l0S4S'></tfoot>
                <tbody id='l0S4S'></tbody>
            • <small id='l0S4S'></small><noframes id='l0S4S'>

                本文介绍了如何在恒定空间中对单链表进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个单向链表,由于内存限制,我需要在恒定空间中对其进行排序(换句话说,不应使用与列表中项目数成正比的额外空间).

                I have a singly linked list and I need to sort it in constant space due to memory limitations (in other words, no extra space should be used that is proportional to the number of items in the list).

                链表的结构为:

                • head.item = 要排序的有效负载;和
                • head.next = 下一项.
                • head.item = the payload you want to sort on; and
                • head.next = the next item.

                对于我构建另一个列表的恒定空间折扣解决方案的要求,我需要就地完成.

                The requirement for constant space discounts solutions where I build another list, I need to do it in-place.

                我该怎么做?

                推荐答案

                在常量空间中对链表进行排序很容易,只需调整指针即可.最简单的方法是使用只交换相邻元素的排序算法.我将提供一个冒泡排序,只是因为你对效率没有要求:

                Sorting a linked list in constant space is easy, you just have to adjust the pointers. The easiest way to do this is to use a sort algorithm that only swaps adjacent elements. I'm going to provide a bubble-sort, just because you've made no requirement for efficiency:

                # Enter loop only if there are elements in list.
                
                swapped = (head <> null)
                while swapped:
                    # Only continue loop if a swap is made.
                
                    swapped = false
                
                    # Maintain pointers.
                
                    curr = head
                    next = curr.next
                    prev = null
                
                    # Cannot swap last element with its next.
                
                    while next <> null:
                        # Swap if items in wrong order.
                
                        if curr.item > next.item:
                            # Notify loop to do one more pass.
                
                            swapped = true
                
                            # Swap elements (swapping head is special case).
                
                            if curr == head:
                                head = next
                                temp = next.next
                                next.next = curr
                                curr.next = temp
                                curr = head
                            else:
                                prev.next = curr.next
                                curr.next = next.next
                                next.next = curr
                                curr = next
                            endif
                        endif
                
                        # Move to next element.
                
                        prev = curr
                        curr = curr.next
                        next = curr.next
                    endwhile
                endwhile
                

                这篇关于如何在恒定空间中对单链表进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:简单的 C++ 链表 下一篇:使用 C++ 递归打印 LinkedList

                相关文章

                <tfoot id='JGHrW'></tfoot>
                <legend id='JGHrW'><style id='JGHrW'><dir id='JGHrW'><q id='JGHrW'></q></dir></style></legend>

                <small id='JGHrW'></small><noframes id='JGHrW'>

                <i id='JGHrW'><tr id='JGHrW'><dt id='JGHrW'><q id='JGHrW'><span id='JGHrW'><b id='JGHrW'><form id='JGHrW'><ins id='JGHrW'></ins><ul id='JGHrW'></ul><sub id='JGHrW'></sub></form><legend id='JGHrW'></legend><bdo id='JGHrW'><pre id='JGHrW'><center id='JGHrW'></center></pre></bdo></b><th id='JGHrW'></th></span></q></dt></tr></i><div id='JGHrW'><tfoot id='JGHrW'></tfoot><dl id='JGHrW'><fieldset id='JGHrW'></fieldset></dl></div>

                    <bdo id='JGHrW'></bdo><ul id='JGHrW'></ul>