Sort List | leetcode

Sort List

  My Submissions

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



#include 
   
   
    
    
using namespace std;
#include 
    
    
     
     


//  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) {
        // IMPORTANT: Please reset any member data you declared, as
        // the same Solution instance will be reused for each test case.
        // 归并排序不解释
        if (head == NULL) {
            return head;
        }
        //求得list 的长度,以便获得一半
        int len = 0;
        ListNode *cur = head;
        while (cur) {
            cur = cur->next;
            len++;
        }
        if (len == 1) { //只有一个节点
            return head;
        }
        int mid_len = len / 2 - 1; //找到前一个节点
        cur = head;
        int index = 0;
        while (index < mid_len) {
            cur = cur->next;
            index++;
        }
        ListNode *first_head = head;
        ListNode *second_head = cur->next;
        cur->next = NULL; //第一个链表最后为空

        //递归调用
        first_head = sortList(first_head);
        second_head = sortList(second_head);
        //合并链表
        ListNode *result_head = NULL;
        ListNode *result_tail = NULL;
        while (first_head && second_head) {
            if (first_head->val < second_head->val) {
                if (result_head == NULL) {
                    result_head = first_head;
                }
                else {
                    result_tail->next = first_head;
                }
                result_tail = first_head;
                first_head = first_head->next;
            }
            else {
                if (result_head == NULL) {
                    result_head = second_head;
                }
                else {
                    result_tail->next = second_head;
                }
                result_tail = second_head;
                second_head = second_head->next;
            }
        }
        while (first_head) { //处理第一个链表剩余部分,由于一定是一半处,所以result_head不为空,不用判断了
            result_tail->next = first_head;
            result_tail = first_head;
            first_head = first_head->next;
        }
        while (second_head) { //处理第二个链表剩余部分,由于一定是一半处,所以result_head不为空,不用判断了
            result_tail->next = second_head;
            result_tail = second_head;
            second_head = second_head->next;
        } 
        return result_head;

    }
};

int main() {
    Solution so;
    ListNode l1(1), l2(2), l3(3), l4(4), l5(5);
    l1.next = &l3;
    l3.next = &l5;
    l5.next = &l4;
    l4.next = &l2;

    ListNode *result = so.sortList(&l1);
    while (result) {
        cout << result->val << " ";
        result = result->next;
    }
}

    
    
   
   

多谢网友( 1楼  wwwwwwking)的评价,参考STL源码,实现了链表排序的非递归版本,根据leetcode最后的测试结果显示,递归版本300ms,非递归版本280ms,相差不是很大。而且非递归理解起来比较困难,最好是实际手动操作五六个元素。代码如下,欢迎指正。
#include 
    
    
     
     
using namespace std;
#include 
     
     
      
      


//  Definition for singly-linked list.
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *merge(ListNode *first_head, ListNode *second_head) {
        ListNode *result_head = NULL;
        ListNode *result_tail = NULL;
        while (first_head && second_head) {
            if (first_head->val < second_head->val) {
                if (result_head == NULL) {
                    result_head = first_head;
                }
                else {
                    result_tail->next = first_head;
                }
                result_tail = first_head;
                first_head = first_head->next;
            }
            else {
                if (result_head == NULL) {
                    result_head = second_head;
                }
                else {
                    result_tail->next = second_head;
                }
                result_tail = second_head;
                second_head = second_head->next;
            }
        }
        while (first_head) { //处理第一个链表剩余部分
            if (result_head == NULL) {
                result_head = first_head;
            }
            else {
                result_tail->next = first_head;
            }
            result_tail = first_head;
            first_head = first_head->next;
        }
        while (second_head) { //处理第二个链表剩余部分
            if (result_head == NULL) {
                result_head = second_head;
            }
            else {
                result_tail->next = second_head;
            }
            result_tail = second_head;
            second_head = second_head->next;
        } 
        return result_head;
    }
    ListNode *sortList(ListNode *head) {
        //280ms
        if (head == NULL || head->next == NULL) { //空节点或者一个节点
            return head;
        }
        ListNode *carry = NULL;
        struct Node {
            ListNode *head;
        };
        Node counter[64] = {0}; //临时链表节点
        for (int i = 0; i < 64; i++) {
            counter[i].head = NULL;
        }
        ListNode *cur = head;
        int fill = 0;
        while (cur) {
            ListNode *next = cur->next; 
            cur->next = NULL;  //每次只有一个节点
            carry = cur; 
            int i = 0;
            while (i < fill && counter[i].head != NULL) {
                counter[i].head = merge(counter[i].head, carry);
                // fill the counter[i].head = NULL //如果满足2的n次方,向前移动指针
                carry = counter[i].head;  
                counter[i].head = NULL;
                i++;
            }

            counter[i].head = carry; //将carry赋值给最高的counter
            carry = NULL;
            if (i == fill) { //满足2的n次方
                fill++;
            }
            cur = next; //下一个节点
        }
        for (int i = 1; i < fill; i++) {
            counter[i].head = merge(counter[i].head, counter[i - 1].head);
        }
        return counter[fill - 1].head;
    }
};

int main() {
    Solution so;
    ListNode l1(1), l2(2), l3(3), l4(4), l5(5);
    l1.next = &l3;
    l3.next = &l5;
    l5.next = &l4;
    l4.next = &l2;
    //1 3 5 4 2 
    ListNode *result = so.sortList(&l1);
    while (result) {
        cout << result->val << " ";
        result = result->next;
    }

}

     
     
    
    


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值