【Sort List】cpp

题目:

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

代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* sortList(ListNode* head) {
        if ( !head || !head->next ) return head;
        ListNode dummy(-1);
        dummy.next = head;
        ListNode *p1=&dummy, *p2=&dummy;
        while ( p2 && p2->next && p2->next->next )
        {
            p1 = p1->next;
            p2 = p2->next->next;
        }
        ListNode *h1 =  Solution::sortList(p1->next);
        p1->next = NULL;
        ListNode *h2 =  Solution::sortList(dummy.next);
        return Solution::mergeTwo(h1, h2);
    }
    static ListNode* mergeTwo(ListNode *h1, ListNode *h2)
    {
        ListNode dummy(-1);
        ListNode *p = &dummy;
        while ( h1 && h2 )
        {
            if ( h1->val<h2->val )
            {
                p->next = h1;
                h1 = h1->next;
            }
            else
            {
                p->next = h2;
                h2 = h2->next;
            }
            p = p->next;
        }
        p->next = h1 ? h1 : h2;
        return dummy.next;
    }
};

tips:

单链表时间要求O(nlongn) 且const extra space,可以选择归并排序(另,双向链表适合用快速排序)

第一次没有AC,原因是少考虑一种返回条件,即“head只有一个元素的时候需要直接返回”,修改之后第二次AC了。

时间: 2024-09-28 16:13:02

【Sort List】cpp的相关文章

【Sort Colors】cpp

题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, an

【Subsets II】cpp

题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If nums = [1,2,2], a sol

leetcode 【 Sort Colors 】python 实现

题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, an

【Interleaving String】cpp

题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = "aabcc",s2 = "dbbca", When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", return false. 代码: “merge sor

leetcode 【 Sort List 】 python 实现

题目: Sort a linked list in O(n log n) time using constant space complexity. 代码:oj 测试通过 Runtime: 372 ms 1 # Definition for singly-linked list. 2 # class ListNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.next = None 6 7 class Solution: 8 #

HDU 1862 EXCEL排序【sort排序】

/* 题目大意:按照EXCEL的要求排序 解题思路:用sort排序 难点详解:如何对字符排序,如何对数字排序 关键点:排序 解题人:lingnichong 解题时间:2014-08-09 17:19:36 解题体会:对理解和熟练运用sort排序,有很大的帮助 */ EXCEL排序 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 131

【Combination Sum 】cpp

题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (including target) w

【Word Break】cpp

题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. For example, givens = "leetcode",dict = ["leet", "code"]. Return true becau

【Sudoku Solver】cpp

题目: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 代码: cla