题解 | #放弃内存求时间 一次过 可以参考 单链表的排序#

单链表的排序

http://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08

/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head ListNode类 the head node
     * @return ListNode类
     */
    ListNode* sortInList(ListNode* head) {
        // write code here
        vector<int> result;
        while(head){
            result.push_back(head->val);
            head = head->next;
        }
        sort(result.begin(), result.end()); ListNode* nohead = new ListNode(NULL);
        for(int i = result.size()-1;i>=0;i--){
            ListNode* p = new ListNode(result[i]);
            p->next = nohead->next;
            nohead->next = p;
        }
        return nohead->next;
    }
};
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务