主要思路:
1 如果為null或者只有乙個節點那麼直接返回;
2 將鍊錶分成兩部分,分別進行排序,形成兩個有序鍊錶;
3 將兩個有序鍊錶合併;
void merge_sort(struct node **list)
void split(struct node *head, struct node **lista, struct node **listb)
struct node *p = head;;
struct node *pfast = p->next;
while (pfast->next != null)
} *lista = head;
*listb = p->next;
p->next = null;
}struct node *mergesortedlink(struct node *lista, struct node *listb)
else if (listb == null) else else
lista = lista->next;
} else else
listb = listb->next;
} }if (lista != null)
if (listb != null)
} return head;
}
鍊錶歸併排序
include include include include include using namespace std typedef int type typedef struct nodetag node node build type a,int n pnode pnext null retu...
鍊錶 歸併排序
時間複雜度o nlogn 空間複雜度o 1 include include 定義鍊錶 typedef struct listnode linklist linklist head null 建立鍊錶 linklist createlist int arr,int len rear next null...
鍊錶歸併排序
題目描述 在o n log n 的時間內使用常數級空間複雜度對鍊錶進行排序。示例1輸入 複製返回值 複製說明 本題目包含複雜資料結構listnode,點此檢視相關資訊 define null null define node listnode class solution int len node ...