qq_37933864
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条回答