148-sort-list
Question
https://leetcode.com/problems/sort-list/description/
Sort a linked list in O(n log n) time using constant space complexity.
Example:
Thought Process
- ArrayList
- Save the value to the array list and sort
- Update the value accordingly
- Time complexity O(n log n)
- Space complexity O(n)
- Divide and Conquer
- Time complexity O(n logn)
- Space complexity O(1) or O(log n) due recursion stack
Solution
class Solution {
public ListNode sortList(ListNode head) {
List<Integer> list = new ArrayList<>();
ListNode cur = head;
while (cur != null) {
list.add(cur.val);
cur = cur.next;
}
Collections.sort(list);
cur = head;
for (int val : list) {
cur.val = val;
cur = cur.next;
}
return head;
}
}
class Solution {
public ListNode sortList(ListNode head) {
if (head == null || head.next == null) return head;
ListNode slow = head, fast = head.next.next;
while (fast!= null && fast.next!= null){
slow = slow.next;
fast = fast.next.next;
}
ListNode l1 = sortList(slow.next);
slow.next = null;
ListNode l2 = sortList(head);
return merge(l1, l2);
}
private ListNode merge(ListNode l1, ListNode l2){
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
while (l1 != null || l2 != null){
if (l1 == null){
cur.next = l2;
break;
} else if (l2 == null){
cur.next = l1;
break;
} else {
if (l1.val < l2.val){
cur.next = l1;
l1 = l1.next;
} else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
}
return dummy.next;
}
}