首页 > 其他 > 详细

Insertion Sort List

时间:2016-07-12 17:30:05      阅读:244      评论:0      收藏:0      [点我收藏+]

对链表进行插入排序,比对数组排序麻烦一点。

技术分享
ListNode *insertSortList(ListNode *head)
      {
          ListNode dummy(-1);
          for (ListNode *cur = head; cur != nullptr;)
          {
              //将当前结点插入到此结点之后
              auto Pos = findPos(&dummy, cur->val);
              //保存当前结点的下一个结点
              ListNode *temp = cur->next;
              //插入
              cur->next = Pos->next;
              Pos->next = cur;
              //继续下一个结点
              cur = temp;
          }
      }

      ListNode *findPos(ListNode *head, int val)
      {
          ListNode *pre = nullptr;
          for (ListNode *cur = head; cur != nullptr&&cur->val <= val; pre = cur, cur = cur->next);

          return pre;
      }
View Code

 

Insertion Sort List

原文:http://www.cnblogs.com/573177885qq/p/5663988.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!