Sort List
2016-12-14 14:11
337 查看
Sort a linked list in O(n log n)
time using constant space complexity.
Merge sort
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param head: The head of linked list.
* @return: You should return the head of the sorted linked list,
using constant space complexity.
*/
public ListNode sortList(ListNode head) {
if(head == null || head.next == null) {
return head;
}
return helper(head);
}
private ListNode helper(ListNode head) {
//base case
if(head == null || head.next == null) {
return head;
}
ListNode middle = findMiddle(head);
ListNode right = helper(middle.next);
middle.next = null;
ListNode left = helper(head);
return merge(left, right);
}
private ListNode findMiddle(ListNode head) {
ListNode slow = head, fast = head.next;
while(fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
private ListNode merge(ListNode left, ListNode right) {
ListNode dummy = new ListNode(0);
ListNode head = dummy;
while(left != null && right != null) {
if(left.val < right.val) {
head.next = left;
left = left.next;
} else {
head.next = right;
right = right.next;
}
head = head.next;
}
if(left != null) {
head.next = left;
}
if(right != null) {
head.next = right;
}
return dummy.next;
}
}
time using constant space complexity.
Merge sort
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param head: The head of linked list.
* @return: You should return the head of the sorted linked list,
using constant space complexity.
*/
public ListNode sortList(ListNode head) {
if(head == null || head.next == null) {
return head;
}
return helper(head);
}
private ListNode helper(ListNode head) {
//base case
if(head == null || head.next == null) {
return head;
}
ListNode middle = findMiddle(head);
ListNode right = helper(middle.next);
middle.next = null;
ListNode left = helper(head);
return merge(left, right);
}
private ListNode findMiddle(ListNode head) {
ListNode slow = head, fast = head.next;
while(fast.next != null && fast.next.next != null) {
fast = fast.next.next;
slow = slow.next;
}
return slow;
}
private ListNode merge(ListNode left, ListNode right) {
ListNode dummy = new ListNode(0);
ListNode head = dummy;
while(left != null && right != null) {
if(left.val < right.val) {
head.next = left;
left = left.next;
} else {
head.next = right;
right = right.next;
}
head = head.next;
}
if(left != null) {
head.next = left;
}
if(right != null) {
head.next = right;
}
return dummy.next;
}
}
相关文章推荐
- LeetCode OJ 292.Nim Gam148. Sort List
- [LeetCode]Sort List
- [LeetCode]Sort List 链表排序 + Merge Two Sorted List 合并两个有序链表
- [zz]c++ list sort方法
- Insertion Sort List
- [LeetCode]Insertion Sort List
- LeetCode147_Insertion Sort List(用插入排序算法对链表进行排序) Java题解
- Leetcode # 148 Sort List
- sort list--LeetCode
- List集合sort
- scala-37::List的foldLeft、foldRight、sort操作代码实战
- LeetCode - Sort List
- Insertion Sort List
- LeetCode(147) Insertion Sort List
- Sort List问题及解法
- leetcode之Insertion Sort List
- Sort List
- java List 排序 Collections.sort()
- Collections.sort对List排序的两种方法
- leetcode.148. Sort List