Sort List

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

 

1、使用快排:交换的次数太多,超过时间限制

  • 选择第一个值为基准值,然后比较
#include <stdio.h>

// Definition for singly-linked list.
 struct ListNode {
    int val;
     struct ListNode *next;
 };

struct ListNode* sortList(struct ListNode* head) {
    struct ListNode *result = NULL, *left = NULL, *right = NULL;
    struct ListNode *p = head;
    struct ListNode *left_p = malloc(sizeof(struct ListNode));
    struct ListNode *right_p = malloc(sizeof(struct ListNode));
    left_p->val = 0;
    right_p->val = 0;
    left_p->next = NULL;
    right_p->next = NULL;
    left = left_p;
    right = right_p;
//基本的分类,如果p为NULL,或者只有一个值的话就可以返回了
    if (p == NULL)
        return NULL;
    else if    (p->next == NULL)
        return head;
    else
        p = head->next;
    while (p != NULL)
    {
        if (p->val < head->val)
        {
            left_p->next = p;
            left_p = left_p->next;
        }
        else
        {
            right_p->next = p;
            right_p = right_p->next;
        }
        p = p->next;
    }
    right_p->next = NULL;
    left_p->next = NULL;
    left_p = sortList(left->next);
    right = sortList(right->next);
    left = left_p;
    if (left_p != NULL)
    {
        while (left_p != NULL)
            left_p = left_p->next;
        left_p->next = head;
    }
    else
        left = head;
    head->next = right;    
    return left;
}
int main()
{
    struct ListNode *head = malloc(sizeof(struct ListNode));
    struct ListNode *head_bk = head;
    int a[] = { 1, 2, 3, 4 };
    for (int i = 0; i < 4; i++)
    {
        struct ListNode *tmp = malloc(sizeof(struct ListNode));
        tmp->val = a[i];
        head->next = tmp;
        head = head->next;
    }
    head->next = NULL;
    
    sortList(head_bk->next);
}

2、使用并归排序

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
 struct ListNode* sortList(struct ListNode* head) {
     struct ListNode *left = NULL, *right = NULL, *result = NULL, *tmp = NULL;
     struct ListNode *oneStep = malloc(sizeof(struct ListNode));
     struct ListNode *twoStep = malloc(sizeof(struct ListNode));
     struct ListNode *one = oneStep, *two = twoStep;
     if (head == NULL)
         return NULL;
     else if (head->next == NULL)
         return head;
     oneStep->next = head;    //这里很重要,备份两个链表的头指针
     twoStep->next = head;    //用于后面的释放内存
     while (twoStep != NULL && twoStep->next != NULL)
     {
         oneStep = oneStep->next;
         twoStep = twoStep->next->next;
     }
     right = sortList(oneStep->next);
     oneStep->next = NULL;
     left = sortList(head);
     tmp = malloc(sizeof(struct ListNode));
     tmp->val = 0;
     tmp->next = NULL;
     result = tmp;
     while (left != NULL && right != NULL)
     {
         if (left->val < right->val)
         {
             tmp->next = left;
             left = left->next;
         }         
         else
         {
             tmp->next = right;
             right = right->next;
         }
         tmp = tmp->next;
         tmp->next = NULL;
     }
     if (left != NULL)
         tmp->next = left;
     if (right != NULL)
         tmp->next = right;
     tmp = result;
     result = result->next;
     free(tmp);
     free(one);
     free(two);
     return result;
 }
  • malloc的空间,不用了记得free掉,由于链表这些指针后来会被移动,所以需要留一个备份,来释放这个头
posted @ 2015-12-04 18:24  dylqt  阅读(167)  评论(0编辑  收藏  举报