qq_37933864
2017-03-16 08:05简单的链表排序的问题
题目:如,给出 1->3->2->null,给它排序变成 1->2->3->null.
写了简单的答案不能通过求帮忙看下错误原因
第一次提问这个格式怎么编辑o(╯□╰)o
/**
- Definition of ListNode
- class ListNode {
- public:
- int val;
- ListNode *next;
- ListNode(int val) {
- this->val = val;
- this->next = NULL;
- }
- }
/
class Solution {
public:
/*
- @param head: The first node of linked list.
- @return: You should return the head of the sorted linked list, using constant space complexity. */ ListNode *sortList(ListNode *head) { // write your code here if(head==NULL) return NULL; if(head->next==NULL) return head; ListNode *dummy = new ListNode(0); ListNode *temp = dummy; vector nums; while(head!=NULL) { nums.push_back(head->val); head = head->next; } sort(nums.begin(),nums.end()); for(int i = 0;i < nums.size();i++) { temp->next->val = nums[i]; temp = temp->next; } temp->next = NULL; return dummy->next; } };
- 点赞
- 回答
- 收藏
- 复制链接分享
1条回答
为你推荐
- C语言中random和time函数的问题
- c
- random
- 算法
- 数据
- 1个回答
- 请问这个对链表进行排序的函数有什么问题?
- c
- 排序
- c++
- 链表
- 2个回答
- java编程,我这段代码是在ecplise上实现的,但DEBUG都没问题,运行后却什么都没有弹出
- java
- eclipse
- 1个回答