BM12单链表的排序

这题太水了,没什么好分析的。

直接看代码吧....

 

/**
 * 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> vec;
//         int len = 0;
        ListNode *p = head;
        
        while(p != nullptr)
        {
            vec.push_back(p->val);
            p = p->next;
        }
        
        sort(vec.begin(),vec.end());
        
        p = head;
        int i = 0;
        while(p != nullptr)
        {
            p->val = vec[i++];
            p = p->next;
        }
        
        return head;
        
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_46120107/article/details/126205044