# 單鏈表的排序問題

CSDN：單鏈表的排序問題

## 題目鏈接

LeetCode 148. Sort List

## 思路一：轉換數組結合快速排序

``````class Solution {
}
int size = 0;
while (cur != null) {
size++;
cur = cur.next;
}
ListNode[] nodes = new ListNode[size];
int i = 0;
while (cur != null) {
nodes[i++] = cur;
cur = cur.next;
}
sortArr(nodes);
return arrToList(nodes);
}

private void sortArr(ListNode[] nodes) {
p(nodes, 0, nodes.length - 1);
}

private void p(ListNode[] arr, int L, int R) {
if (L >= R) {
return;
}
swap(arr, L + (int) (Math.random() * (R - L + 1)), R);
int[] equalArea = netherlandsFlag(arr, L, R);
p(arr, L, equalArea[0] - 1);
p(arr, equalArea[1] + 1, R);
}

private int[] netherlandsFlag(ListNode[] nodes, int L, int R) {
if (L > R) {
return new int[]{-1, -1};
}
if (L == R) {
return new int[]{L, R};
}
int less = L - 1;
int more = R;
ListNode num = nodes[R];
for (int i = L; i < more; i++) {
if (nodes[i].val < num.val) {
swap(nodes, ++less, i);
} else if (nodes[i].val > num.val) {
swap(nodes, i--, --more);
}
}
swap(nodes, R, more);
return new int[]{less + 1, more};
}

public void swap(ListNode[] nodes, int i, int j) {
if (i != j) {
ListNode t = nodes[i];
nodes[i] = nodes[j];
nodes[j] = t;
}
}

public ListNode arrToList(ListNode[] nodes) {
for (int i = 1; i < nodes.length; i++) {
cur.next = nodes[i];
cur = nodes[i];
}
cur.next = null;
}
}
``````

## 思路二：使用歸併排序

`[0……1]`,`[2……3]`,`[4……5]`,`[6……7]`,`[8……8]`

`1->3`,`2->4`,`5->6`,`4->6`,`8`

`[0……3]`,`[4……7]`,`[8……8]`

`1->2->3->4`,`4->5->6->6`,`8`

`[0……7]`,`[8……8]`

`1->2->3->4->4->5->6->6`,`8`

`1->2->3->4->4->5->6->6->8`

``````class Solution {
int N = 0;
while (cur != null) {
N++;
cur = cur.next;
}
ListNode pre = null;
int L = 1;
while (L < N) {
while (teamFirst != null) {
ListNode[] hthtn = hthtn(teamFirst, L);
ListNode[] mhmt = merge(hthtn[0], hthtn[1], hthtn[2], hthtn[3]);
if (h == teamFirst) {
h = mhmt[0];
pre = mhmt[1];
} else {
pre.next = mhmt[0];
pre = mhmt[1];
}
teamFirst = hthtn[4];
}
teamFirst = h;
pre = null;
L <<= 1;
}
return h;
}

public  ListNode[] hthtn(ListNode teamFirst, int len) {
ListNode ls = teamFirst;
ListNode le = teamFirst;
ListNode rs = null;
ListNode re = null;
ListNode next = null;
int p = 0;
while (teamFirst != null) {
// 之所以這裏是小於等於，是因爲這裏可能不滿足分組的個數（不足個數）
if (p <= len - 1) {
le = teamFirst;
}
if (p == len) {
rs = teamFirst;
}
if (p > len - 1) {
re = teamFirst;
}
if (p == (len << 1) - 1) {
break;
}
p++;
teamFirst = teamFirst.next;
}
if (le != null) {
le.next = null;
}
if (re != null) {
next = re.next;
re.next = null;
}
return new ListNode[]{ls, le, rs, re, next};
}

// 返回merge後的頭和尾
// 注意邊界考慮
public  ListNode[] merge(ListNode h1, ListNode t1, ListNode h2, ListNode t2) {
if (h2 == null) {
return new ListNode[]{h1, t1};
}
ListNode tail = h1;
ListNode c = null;
ListNode pre = null;
while (h1 != t1.next && h2 != t2.next) {
if (h1.val > h2.val) {
c = h2;
h2 = h2.next;
} else {
c = h1;
h1 = h1.next;
}
if (pre == null) {
// 後續就由pre去往下插入節點
pre = c;

} else {
pre.next = c;
pre = c;
}
}
// h1節點沒越界
if (h1 != t1.next) {
while (h1 != t1.next) {
pre.next = h1;
pre = pre.next;
tail = h1;
h1 = h1.next;
}
} else {
while (h2 != t2.next) {
pre.next = h2;
pre = pre.next;
tail = h2;
h2 = h2.next;
}
}