链表的归并排序

xiaolomg 2014-03-23 04:37:46
下午无聊做了个链表的归并排序,代码测试有问题,但找了半天没看出来,哪位大神给看看?
#include <string>
#include <vector>
#include <stack>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};

void print(ListNode* node){
while (node){
printf("%d ", node->val);
node = node->next;
}
printf("\n");
}

class Solution {
public:
ListNode *sortList(ListNode *head) {
if (!head){
return NULL;
}

if (!head->next){
return head;
}

ListNode* after = devide(head);

print(head);
print(after);

ListNode* res1 = sortList(head);
ListNode* res2 = sortList(after);

return merge(res1, res2);
}

ListNode * devide(ListNode *head){
ListNode* slow = head;
ListNode* quick = head;

while (quick){
quick = quick->next;

if (quick){
quick = quick->next;
slow = slow->next;
}
}

ListNode* res = slow->next;
slow->next = NULL;

return res;
}

ListNode* merge(ListNode *left, ListNode *right){
if (!left && !right){
return NULL;
}

if (left && !right){
return left;
}

if (!left && right){
return right;
}

ListNode* resHead = NULL;
ListNode* res = NULL;
while (left && right){
ListNode* tmp = NULL;
if (left->val < right->val){
tmp = left;
left = left->next;
}
else{
tmp = right;
right = right->next;
}

if (!res){
resHead = tmp;
res = tmp;
}
else{
res->next = tmp;
res = res->next;
}
}

if (left){
res->next = left;
}

if (right){
res->next = right;
}

return resHead;
}
};

void insert(ListNode*& node, int val){
ListNode* newNode = new ListNode(val);
ListNode* tmpNode = node;
if (!node){
node = newNode;
}
else{
while (tmpNode->next){
tmpNode = tmpNode->next;
}

tmpNode->next = newNode;
}
}


int main(){
ListNode* head = NULL;
insert(head, 2);
insert(head, 1);
insert(head, 3);
//print(head);
Solution s;
s.sortList(head);
//print(head);
}
...全文
144 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
logiciel 2014-03-26
  • 打赏
  • 举报
回复
#include <string>
#include <vector>
#include <stack>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

struct ListNode {
  int val;
  ListNode *next;
  ListNode(int x) : val(x), next(NULL) {}
};

void print(ListNode* node){
  while (node){
    printf("%d ", node->val);
    node = node->next;
  }
  printf("\n");
}

class Solution {
public:
  ListNode *sortList(ListNode *head) {
    if (!head){
      return  NULL;
    }

    if (!head->next){
      return head;
    }

    ListNode* after = devide(head);

    print(head);
    print(after);

    ListNode* res1 = sortList(head);
    ListNode* res2 = sortList(after);

    return merge(res1, res2);
  }

  ListNode * devide(ListNode *head){
    ListNode* slow = head;
    ListNode* quick = head;

    while (quick){
      quick = quick->next;

      if (quick){
        quick = quick->next;
        slow = slow->next;
      }
    }

    ListNode* res = slow->next;
    if (NULL == res)     //加 处理只有2个节点的情况
    {                    //加
      res = slow;        //加
      head->next = NULL; //加
    }                    //加
    else                 //加
    {
      slow->next = NULL;
    }

    return res;
  }

  ListNode* merge(ListNode *left, ListNode *right){
    if (!left && !right){
      return NULL;
    }  

    if (left && !right){
      return left;
    }

    if (!left && right){
      return right;
    }

    ListNode* resHead = NULL;
    ListNode* res = NULL;
    while (left && right){
      ListNode* tmp = NULL;
      if (left->val < right->val){
        tmp = left;
        left = left->next;
      }
      else{
        tmp = right;
        right = right->next;
      }

      if (!res){
        resHead = tmp;
        res = tmp;
      }
      else{
        res->next = tmp;
        res = res->next;
      }
    }

    if (left){
      res->next = left;
    }

    if (right){
      res->next = right;
    }

    return resHead;
  }
};

void insert(ListNode*& node, int val){
  ListNode* newNode = new ListNode(val);
  ListNode* tmpNode = node;
  newNode->next = NULL;//加
  if (!node){
    node = newNode;
  }
  else{
    while (tmpNode->next){
      tmpNode = tmpNode->next;
    }

    tmpNode->next = newNode;
  }
}


int main(){
  ListNode* head = NULL;
  insert(head, 2);
  insert(head, 1);
  insert(head, 3);
  print(head);
  Solution s;
  head = s.sortList(head);//改 s.sortList(head);
  print(head);
}
bobo928843007 2014-03-25
  • 打赏
  • 举报
回复

insert(ListNode* & node, int val)
报错什么意思,要改成下面的吗

insert(ListNode*  node, int val)

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧