一. 鏈表 4 鏈表排序

問題:用插入排序?qū)︽湵砼判?/p>

思路:使用鏈表中開頭的兩個(gè)node,建立一個(gè)新的鏈表。然后依次從舊的鏈表第三個(gè)node開始取值,進(jìn)行插入排序。

Python3

"""
Definition of ListNode
class ListNode(object):

    def __init__(self, val, next=None):
        self.val = val
        self.next = next
"""
class Solution:
    """
    @param head: The first node of linked list.
    @return: The head of linked list.
    """ 
   def insertionSortList(self, head):
        if head == None and head.next == None:
            return head
        
        # a flexible point of ranked list
        ranked_point = head
        # a flexible point of raw list
        raw_point = head.next
        point = raw_point
        # seperate the list into 2 lists
        head.next = None
        
        while raw_point != None:
            
            while ranked_point != None: 
                
                # if new node is smaller than current node
                if raw_point.val <= ranked_point.val:
                    # jump to next node of the raw list
                    raw_point = raw_point.next
                    # insert this new node into the list
                    point.next = ranked_point
                    
                    # if the new node is smallest one 
                    if ranked_point == head:
                        # refresh the head 
                        head = point
                    point = raw_point
                    break
                
                # if the new node is the largest one in ranked list
                if ranked_point.next == None:
                    # insert it into the tail of the ranked list
                    ranked_point.next = raw_point
                    # jump to next node of the raw list
                    raw_point = raw_point.next
                    
                    ranked_point.next.next = None
                    break         
                ranked_point = ranked_point.next 
            ranked_point = head
        return head

思考:

  1. 在建立新的鏈表過程中,為了減少空間復(fù)雜度,我沒有新建node,而是將原node進(jìn)行修改。這就牽涉到了對(duì)象實(shí)例的引用。我們可以通過引用來修改對(duì)象實(shí)例的值,但是要知道一旦修改,這個(gè)node的其他引用也會(huì)被修改。所以我們要將跳到下一個(gè)node的行為提前運(yùn)行,不然一旦被修改,next指針就會(huì)失去原目標(biāo)。

  2. 在一開始的時(shí)候,我本來準(zhǔn)備用通用的程序來表達(dá)所有的特殊情況,例如在新的鏈表頭插入node,在新的鏈表尾插入node。但是最后都不得不拿出來單獨(dú)處理,可能是我沒想到吧,如有望告知。

Java

個(gè)人感覺任何程序一旦變成java就會(huì)復(fù)雜,我寫了幾次都存在超出內(nèi)存的情況,最后再網(wǎng)路上看到別人寫的,比我的要簡單很多,而且邏輯清晰,于是我就當(dāng)了一次代碼的搬運(yùn)工。。。原碼在這里

/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */ 
public class Solution {
    /**
     * @param head: The first node of linked list.
     * @return: The head of linked list.
     */
    public ListNode insertionSortList(ListNode head) {  
        // incase that the list is null or only one element
        if (head == null || head.next == null)  
            return head;  
        //未排序游動(dòng)指針C
        ListNode c = head.next;    
        // break the list into 2 list, one is ranked list; another one is the raw list.
        head.next = null;    
        ListNode pt, h;   //pt:臨時(shí)節(jié)點(diǎn)指針,h:已排序部分游動(dòng)指針  
  
        while (c != null) {  
            // jump to the next node
            pt = c;  
            c = c.next;  
            // make the new node link to null
            pt.next = null;  
           // point h to the head
            h = head;
  
            if (head.val > pt.val) { //比較頭部  
                pt.next = head;  
                head = pt;  
                continue;  
            }  
   
            while (h.next != null) { //比較有序部分  
                if (h.next.val > pt.val) {  
                    pt.next = h.next;  
                    h.next = pt;  
                    break;  
                }  
                // jump to the next node 
                h = h.next;  
            }  
  
            if (h.next == null) { //追加末尾  
                h.next = pt;  
            }  
        }  
        return head;  
    }  
}  

對(duì)比這兩種思路,一個(gè)是一邊寫,一邊處理出現(xiàn)的例外;一個(gè)是在寫之前就考慮好會(huì)出現(xiàn)的例外并拿出來單獨(dú)處理。

我感覺兩種方法都有各自的利弊,如若兩者結(jié)合起來,即以第二種為主,第一種為輔,這樣可能會(huì)好很多。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,734評(píng)論 18 399
  • 一、基本數(shù)據(jù)類型 注釋 單行注釋:// 區(qū)域注釋:/* */ 文檔注釋:/** */ 數(shù)值 對(duì)于byte類型而言...
    龍貓小爺閱讀 4,455評(píng)論 0 16
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,598評(píng)論 19 139
  • 白露清輝,香霧飄浮。微霜染,翠葉紅榴。流年轉(zhuǎn)眼,歲月神偷。看風(fēng)如馬,云如髻,月如勾。 身衰何懼?初心不老,任霜華,...
    瑾檀yuying閱讀 855評(píng)論 34 36
  • javaScript(01) 簡單的數(shù)據(jù)類型:- String(字符串);- Number(數(shù)字);- Boole...
    小飛蟻閱讀 149評(píng)論 0 0

友情鏈接更多精彩內(nèi)容