时间复杂度要求为o(N log N)所以,排序方法采用归并排序
代码如下
package sort;
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
next = null;
}
}
public class TestListNode {
public ListNode sortList(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode mid=getMid(head);
ListNode midNext=mid.next;
mid.next=null;//将链表断开
return margeSort(sortList(head),sortList(midNext));
}
private ListNode margeSort(ListNode sortList1, ListNode sortList2) {
//定义头结点
ListNode head = new ListNode(0);
//临时节点
ListNode temp =head;
ListNode n1 = sortList1;
ListNode n2 = sortList2;
while(n1!=null&&n2!=null){
if(n1.val<n2.val){
temp.next=n1;
n1=n1.next;
}else{
temp.next=n2;
n2=n2.next;
}
temp=temp.next;
}
temp.next=n1==null?n2:n1;
//去掉没用的头结点
return head.next;
}
private ListNode getMid(ListNode head) {
if(head==null||head.next==null){
return head;
}
//利用快慢指针去找到 链表的中间节点
ListNode fase = head;
ListNode slow = head;
while(fase.next!=null&&fase.next.next!=null){
slow=slow.next;
fase=fase.next.next;
}
return slow;
}
}