lintcode-medium-Sort List

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

Example

Given 1-3->2->null, sort it to 1->2->3->null.

/**
 * 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) {
        // write your code here

        if(head == null || head.next == null)
            return head;

        ListNode slow = head;
        ListNode fast = head;

        while(fast != null && fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }

        ListNode head2 = slow.next;
        slow.next = null;

        head = sortList(head);
        head2 = sortList(head2);

        head = merge(head, head2);

        return head;
    }

    public ListNode merge(ListNode head1, ListNode head2){

        if(head1 == null)
            return head2;
        if(head2 == null)
            return head1;

        ListNode fakehead = new ListNode(0);
        ListNode p = fakehead;
        ListNode p1 = head1;
        ListNode p2 = head2;

        while(p1 != null || p2 != null){

            int num1 = Integer.MAX_VALUE;
            int num2 = Integer.MAX_VALUE;

            if(p1 != null)
                num1 = p1.val;
            if(p2 != null)
                num2 = p2.val;

            if(num1 < num2){
                p.next = p1;
                p = p.next;
                p1 = p1.next;
            }
            else{
                p.next = p2;
                p = p.next;
                p2 = p2.next;
            }

        }

        return fakehead.next;
    }

}
时间: 2024-10-11 13:54:59

lintcode-medium-Sort List的相关文章

[Lintcode]98. Sort List/[Leetcode]148. Sort List

98. Sort List/148. Sort List 本题难度: Medium Topic: Linked List Description Sort a linked list in O(n log n) time using constant space complexity. Example Example 1: Input: 1->3->2->null Output: 1->2->3->null Example 2: Input: 1->7->2

LintCode 463 Sort Integer

这个是O(n2)的排序的总结 /* bubble sort */public static void sortIntegers(int[] A) { // Write your code here int len = A.length; if (len == 0) return; for (int j = 0; j < len; ++j) { for (int i = 0; i < len-1-j; ++i) { // len-1-j if (A[i] > A[i+1]) { int t

[lintcode medium]Anagrams

Anagrams Given an array of strings, return all groups of strings that are anagrams. Example Given ["lint", "intl", "inlt", "code"], return ["lint", "inlt", "intl"]. Given ["ab"

[lintcode medium]4 sum

4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Example Given array S = {1 0 -1 0 -2 2}, and target = 0. A solution

[lintcode medium]Palindrome Linked List

Palindrome Linked List Implement a function to check if a linked list is a palindrome. Example Given 1->2->1, return true Challenge Could you do it in O(n) time and O(1) space? //// 1\find out the medium index of Linked list 2\ reverse the right par

[LintCode] Insertion Sort List

Insertion Sort List Sort a linked list using insertion sort. Example Given 1->3->2->0->null, return 0->1->2->3->null. SOLUTION: 插入排序的思路就是从头开始扫过去,然后遇到比当前点小的点,就把这个点插到当前点之前.插入之后这次操作就完了,从头再扫一遍,直到扫到最后一个点位置. 代码: /** * Definition for List

Lintcode 49 Sort Letters by Case

Given a string which contains only letters. Sort it by lower case first and upper case second. Notice It's NOT necessary to keep the original order of lower-case letters and upper case letters. Example For "abAcD", a reasonable answer is "a

[LintCode] Wiggle Sort 扭动排序

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... Notice Please complete the problem in-place. ExampleGiven nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4].

[lintcode medium] digit counts

Digit Counts Count the number of k's between 0 and n. k can be 0 - 9. Example if n=12, k=1 in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], we have FIVE 1's (1, 10, 11, 12) class Solution { /* * param k : As description. * param n : As description. * r

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,