前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Sort List

Sort List

作者头像
Tyan
发布2019-05-25 22:51:03
1.4K0
发布2019-05-25 22:51:03
举报
文章被收录于专栏:SnailTyanSnailTyan

1. Description

Sort List
Sort List

2. Solution

  • Version 1
代码语言:javascript
复制
/**
 * 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) {
            return NULL;
        }
        vector<ListNode*> nodes;
        ListNode* current = head;
        while(current) {
            int index = binarySearch(nodes, current->val);
            nodes.insert(nodes.begin() + index, current);
            current = current->next;
        }
        ListNode* result = nodes[0];
        current = nodes[0];
        for(int i = 1; i < nodes.size(); i++) {
            current->next = nodes[i];
            current = current->next;
        }
        current->next = NULL;
        return result;
    }

private:
    int binarySearch(vector<ListNode*>& nodes, int target) {
        int left = 0;
        int right = nodes.size() - 1;
        while(left <= right) {
            int mid = (left + right) / 2;
            if(nodes[mid]->val > target) {
                right = mid - 1;
            }
            else if(nodes[mid]->val < target) {
                left = mid + 1;
            }
            else {
                return mid;
            }
        }
        return left;
    }
};
  • Version 2
代码语言:javascript
复制
/**
 * 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* pre = nullptr;
        ListNode* slow = head;
        ListNode* fast = head;
        while (fast != nullptr && fast->next != nullptr) {
            pre = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        pre->next = nullptr;
        ListNode* l1 = sortList(head);
        ListNode* l2 = sortList(slow);
        return mergeTwoLists(l1, l2);
    }

private:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* ptrFirst = l1;
        ListNode* ptrSecond = l2;
        ListNode* head = new ListNode(0);
        ListNode* current = head;
        while(ptrFirst && ptrSecond) {
            if(ptrFirst->val > ptrSecond->val) {
                current->next = ptrSecond;
                ptrSecond = ptrSecond->next;
            }
            else {
                current->next = ptrFirst;
                ptrFirst = ptrFirst->next;
            }
            current = current->next;
        }
        if(ptrFirst) {
            current->next = ptrFirst;
        }
        if(ptrSecond) {
            current->next = ptrSecond;
        }
        return head->next;
    }
};

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年08月16日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. Description
  • 2. Solution
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档