# 148. 排序链表

## 题目描述

• 你可以在 O(n log n) 时间复杂度和常数级空间复杂度下，对链表进行排序吗？

• 链表中节点的数目在范围 [0, 5 * 104] 内
• -105 <= Node.val <= 105

## 思路

/**
* public class ListNode {
*     int val;
*     ListNode next;
*     ListNode() {}
*     ListNode(int val) { this.val = val; }
*     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
int len = 0;
while(tem != null){
len++;
tem = tem.next;
}
for(int i = 1;i < len;i<<=1){
while(cur != null){
for(int j = 1;j < i && cur.next != null;++j){
cur = cur.next;
}
cur.next = null;
for(int j = 1;j < i && cur != null && cur.next != null;++j){
cur = cur.next;
}
ListNode next = null;
if(cur != null){
next = cur.next;
cur.next = null;
}
pre.next = merge;
while(pre.next != null){
pre = pre.next;
}
cur = next;
}
}
}

while(cur1 != null && cur2 != null){
if(cur1.val < cur2.val){
tem.next = cur1;
cur1 = cur1.next;
}else{
tem.next = cur2;
cur2 = cur2.next;
}
tem = tem.next;
}
if(cur1 != null){
tem.next = cur1;
}else if(cur2 != null){
tem.next = cur2;
}