基于链表的归并排序

归并排序基本介绍

归并排序排序是一种常用的时间复杂度为O(n*lg n)的算法,它的基本想法是先对数据不断二分,然后对分开的每段数据进行合并,利用递归的思想,很容易完成排序。归并排序又分为自顶向下和自底向上两种实现方式,自顶向下实现起来比较简单,就是递归,对数据进行二分和合并。对与自底向上而言,,则需要考虑到每次归并元素的个数。
这里要注意的是,对与每次归并来说,必须保证链表的完整性,不能出现链表断裂的情况。

JAVA代码实现

1.自顶向下

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode sortList(ListNode head) {
        if(head==null||head.next==null)
            return head;
        ListNode pro =null;
        ListNode slow = head;
        ListNode fast = head;

        while(fast!=null&&fast.next!=null){
            pro = slow;
            slow = slow.next;
            fast = fast.next.next;
        }
        pro.next = null;

        ListNode l1 = sortList(head);
        ListNode l2 = sortList(slow);

        return merge(l1,l2);
    }

    public ListNode merge(ListNode l1, ListNode l2) {
        if(l1==null) return l2;
        if(l2==null) return l1;

        ListNode dummy = new ListNode(0);
        ListNode cur = dummy;

        while(l1!=null||l2!=null){
            int c1 = Integer.MAX_VALUE;
            int c2 = Integer.MAX_VALUE;

            if(l1!=null) c1 = l1.val;
            if(l2!=null) c2 = l2.val;

            if(c1<c2){
                cur.next = l1;
                cur = l1;
                l1 = l1.next;
            }else{
                cur.next = l2;
                cur = l2;
                l2 = l2.next;
            }
        }

        return dummy.next;
    }
}

2.自底向上

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode sortList(ListNode head) {
      if(head==null||head.next==null)
        return head;

      ListNode cur = head;    
      int count = 0;

      while(cur!=null){
          cur = cur.next;
          count++;
      }

      ListNode dummy = new ListNode(0);
      dummy.next = head;

      for(int step=1;step<count;step +=step){
          cur = dummy.next;
          ListNode left = null;
          ListNode right = null;
          ListNode end = dummy;

          while(cur!=null){
              ListNode t = null;
              left = cur;
              for(int i=0;cur!=null&i<step;i++){
                  t = cur;
                  cur = cur.next;
              }
              t.next = null;

              right = cur;
              //把end作为下一段代码开始的头节点
              for(int i=0;cur!=null&i<step;i++){
                  t = cur;
                  cur = cur.next;
              }
              t.next = null;

              end = merge(left,right,end);
          }
      }

      return dummy.next;
  }
    public  ListNode merge(ListNode l1, ListNode l2,ListNode dummy) {
        if(l1==null){
            dummy.next = l2;
            return null;
        }

        if(l2==null) {
            dummy.next = l1;
            return null;
        }

        ListNode cur = dummy;

        while(l1!=null||l2!=null){
            int c1 = Integer.MAX_VALUE;
            int c2 = Integer.MAX_VALUE;

            if(l1!=null) c1 = l1.val;
            if(l2!=null) c2 = l2.val;

            if(c1<c2){
                cur.next = l1;
                cur = l1;
                l1 = l1.next;
            }else{
                cur.next = l2;
                cur = l2;
                l2 = l2.next;
            }
        }

        return cur;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值