刷题第10天(LeetCode #148.排序链表)

在这里插入图片描述
思路分析:

利用一个指针ptr进行遍历,每次循环遍历过整个链表后,取出链表中节点的最小值连接到一个新的链表res后,同时将取最小值的节点值置为INT_MAX。

代码实现:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution 
{
public:
    ListNode* sortList(ListNode* head) 
    {
        ListNode *res=new ListNode,*h=res,*ptr,*i=head,*k;
        while(i!=nullptr)
        {
            int min=INT_MAX;
            ptr=head;
            while(ptr!=nullptr)
            {
                if(min>ptr->val)
                {
                    min=ptr->val;
                    k=ptr;
                }
                ptr=ptr->next;
            }
            k->val=INT_MAX;
            res->next=new ListNode;
            res=res->next;
            res->val=min;
            i=i->next;
        }
        return  h->next;
    }
};

问题分析:

此思路想法没有问题,但是时间复杂度太高,可以解决较短的链表的排序问题,但当leetcode给出一串长达50000个节点的链表时,终于超出了时间限制。

换种思路:

利用快慢指针,将链表分成两部分分别进行排序。
利用递归让链表不断切分,分链表中只剩下一个元素时,不必再进行切分。
利用合并两个有序链表的思路,对切分后的链表进行重新合并。
最后返回合并后的链表。

代码实现:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution 
{
public:
    ListNode* sortList(ListNode* head) 
    {
        return sortList(head, nullptr);
    }

    ListNode* sortList(ListNode* head, ListNode* tail) 
    {
        if (head == nullptr) 
            return head;
        if (head->next == tail) 
        {
            head->next = nullptr;
            return head;
        }
        ListNode* slow = head, *fast = head;
        while (fast != tail) 
        {
            slow = slow->next;
            fast = fast->next;
            if (fast != tail) 
                fast = fast->next;
        }
        ListNode* mid = slow;
        return merge(sortList(head, mid), sortList(mid, tail));
    }

    ListNode* merge(ListNode* head1, ListNode* head2) 
    {
        ListNode* dummyHead = new ListNode(0);
        ListNode* temp = dummyHead, *temp1 = head1, *temp2 = head2;
        while (temp1 != nullptr && temp2 != nullptr) 
        {
            if (temp1->val <= temp2->val) 
            {
                temp->next = temp1;
                temp1 = temp1->next;
            } 
            else 
            {
                temp->next = temp2;
                temp2 = temp2->next;
            }
            temp = temp->next;
        }
        if (temp1 != nullptr) 
            temp->next = temp1;
        else if (temp2 != nullptr) 
            temp->next = temp2;
        return dummyHead->next;
    }
};

运行结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值