[Leetcode 148, medium] Sort List

Problem:

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

Analysis:

This solution adopts divide-and-conqure method. The implementation is straightforward except some tricks. These tricks are taken here because to avoid the gap of differences between linked-list and array. 

Different with linked-list, we can access any element of an array with its index in equal length of time. In more usual cases, we need to compute indices by some formula or relation.This does not apply to the linked-list case. Every time when we need an elmement in a linked-list, we need run a pointer of node of a linked-list from its head to its tail and stop when the key of a node is equal to that we want.This will greatly increase the time cost in the process of recursion. (O(n) time cost) In the solution of this problem, we use three seperately new linked-lists (all of them with two pointers, one to head and the other one to the tail) to store the elements which are smaller/equal/larger to a given value (the value of the head node). This spends O(1) time to get the tail of a linked-list and combine them to the list we need as a return value.

Solution:

C++:

void AddNode(ListNode*& begin, ListNode*& end, ListNode* elem)
    {
        if(begin == NULL) {
            begin = elem;
            end = begin;
        } else {
            end->next = elem; 
            end = end->next;
        }
    }

    void SortListImpl(ListNode*& begin, ListNode*& end)
    {
        if(begin == NULL)
            return;
        
        ListNode* lessList = NULL;
        ListNode* lessList_tail = NULL;
        ListNode* equalList = NULL;
        ListNode* equalList_tail = NULL;
        ListNode* largerList = NULL;
        ListNode* largerList_tail = NULL;
        int bar = begin->val;
        for(ListNode* cur = begin; cur != NULL; ) {
            ListNode* temp = cur;
            cur = cur->next;
            temp->next = NULL;
            if(temp->val < bar)
                AddNode(lessList, lessList_tail, temp);
            else if(temp->val == bar)
                AddNode(equalList, equalList_tail, temp);
            else
                AddNode(largerList, largerList_tail, temp);
        }
        
        if(lessList != NULL && lessList != lessList_tail)
            SortListImpl(lessList, lessList_tail);
            
        if(largerList != NULL && largerList != largerList_tail)
            SortListImpl(largerList, largerList_tail);
            
        if(lessList != NULL)
            lessList_tail->next = equalList;
        else
            lessList = equalList;
        
        equalList_tail->next = largerList;
        
        begin = lessList;
        end = largerList != NULL ? largerList_tail : equalList_tail;
    }

    ListNode *sortList(ListNode *head) {
        if(head == NULL || head->next == NULL)
            return head;
        
        ListNode* begin = head;
        ListNode* end = head;
        while(end->next != NULL)
            end = end->next;
        
        SortListImpl(begin, end);
        return begin;
    }
Java :


Python:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值