148. Sort List (链表)

来源:互联网 发布:年轻人做淘宝浪费青春 编辑:程序博客网 时间:2024/06/06 00:44

https://leetcode.com/problems/sort-list/description/

题目: 链表排序

思路: 我直接对值进行排序。。。

/** * 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) {        ListNode* temp=head;        vector<int>v;        while(temp!=NULL)        {            v.push_back(temp->val);            temp=temp->next;        }        sort(v.begin(),v.end());        temp=head;int sum=0;        while(temp!=NULL)        {            temp->val=v[sum];            sum++;            temp=temp->next;        }        return head;    }};
原创粉丝点击