# leetCode---链表专题

## 题目描述1    单链表排序问题

Sort a linked list in O(n log n) time using constant space complexity.

public class Solution {
}  //常规合并排序思路
ListNode midNext = mid.next;
}
private ListNode getMid(ListNode head) {         //使用快慢指针获取中间节点位置
}
while(quick.next != null && quick.next.next != null) {
slow = slow.next;
quick = quick.next.next;
}
return slow;
}
private ListNode mergeSort(ListNode n1, ListNode n2) {    对两个分链表进行合并
ListNode preHead = new ListNode(0), cur1 = n1, cur2 = n2, cur = preHead;
while(cur1 != null && cur2 != null) {
if(cur1.val < cur2.val) {
cur.next = cur1;
cur1 = cur1.next;
} else {
cur.next = cur2;
cur2 = cur2.next;
}
cur = cur.next;
}
cur.next = cur1 == null ? cur2 : cur1;     //看哪个链表不为空 继续接上
}
}

public class Solution {
}

public static void quickSort(ListNode head, ListNode end) {
quickSort(partion.next, end);
}
}

public static ListNode partion(ListNode head, ListNode end) {
while (fast != end) {
slow = slow.next;
int temp=slow.val;     //交换slow和fast的值
slow.val=fast.val;
fast.val=temp;
}
fast = fast.next;
}
}