<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>

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

      How can I sort a singly linked list in constant space?(如何在恒定空间中对单链表进行排序?)
    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
                

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

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Is Type(::x); valid?(是类型(::x);有效的?)
                Difference between an inline function and static inline function(内联函数和静态内联函数的区别)
                Compilation fails randomly: quot;cannot open program databasequot;(编译随机失败:“无法打开程序数据库)
                Too many initializers error for a simple array in bcc32(bcc32 中的简单数组的初始值设定项过多错误)
                No Member named stoi in namespace std(命名空间 std 中没有名为 stoi 的成员)
                Error using a constexpr as a template parameter within the same class(在同一个类中使用 constexpr 作为模板参数时出错)
                <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>
                          <tbody id='JGHrW'></tbody>