# 二、归并排序

/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode(int x) { val = x; }
* }
*/
class Solution {
return null;
}
}

private ListNode split(ListNode node) {
if (null == node || null == node.next) {
return node;
}

// 双指针，标记链表中点
ListNode slow = node;
ListNode fast = node.next;
while (null != fast && null != fast.next) {
slow = slow.next;
fast = fast.next.next;
}

// 链表分割
ListNode pre = node;
ListNode post = slow.next;
slow.next = null;

// 递归分割
ListNode left = split(pre);
ListNode right = split(post);
return merge(left, right);
}

private ListNode merge(ListNode pre, ListNode post) {
ListNode node = new ListNode(-1);
ListNode cur = node;
while (null != pre && null != post) {
if (pre.val <= post.val) {
cur.next = new ListNode(pre.val);
pre = pre.next;
} else {
cur.next = new ListNode(post.val);
post = post.next;
}
cur = cur.next;
}

if (null != pre) {
cur.next = pre;
}

if (null != post) {
cur.next = post;
}
return node.next;
}
}
• 时间复杂度：O(nlog(n))
• 空间复杂度：O(log(n))

11-18 4万+

05-21 51

03-27 1074

10-02 4501

01-26 1万+

04-18 2万+

04-23 120

11-21 7491

07-17 8532

02-21 304

08-22 1291

01-10 511

11-09 645

05-07 1267

04-18 2358

09-10 450

08-12 326

07-21 249

07-04 2186

08-03 1700

#### 算法-合并两个排序的链表

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客