单链表归并排序

核心部分代码:

 public Node linkMergeSort(Node head , int length){
  
  if(length==1)
   return head;
  else{
   int len1 = length/2;
   Node head1 = head;
   Node head2 = head;
   for(int i = 1;i< len1;i++){
    head2 = head2.next;
   }
   Node temp = head2;
   head2 = head2.next;
   temp.next = null;
   
   int len2 = length - len1;
   
   //归下去
   head1 = linkMergeSort(head1, len1);
   head2 = linkMergeSort(head2, len2);
   /////////////////////////////////////////////////////////////////
   //并起来
   Node c1;
   Node c2;
   if(head1.value<head2.value){
    head = head2;
    c1 = head1;
    c2 = head2.next;
   }
   else{
    head = head1;
    c1 = head1.next;
    c2 = head2;
   }
   Node current = head;
   while(c1!=null&&c2!=null){
    if(c1.value>=c2.value){
 
     current.next = c1;
     current =c1;
     c1 = c1.next;
     
    }
    else{
     current.next = c2;
     current =c2;
     c2 =c2.next;
    }
    
   }
   if(c1==null)
    current.next = c2;
   else {
    current.next = c1;
   }
   return head;
  }
 }

 

//////////所有相关代码,单链表相关的操作及主程序测试////////////////////

public class SingleLinkRelated {
 
 private Node head;
 
 private int size;
 
 public int getSize() {
  return size;
 }


 public Node getHead() {
  return head;
 }


 public void setHead(Node head) {
  this.head = head;
 }


 public static void main(String[] args) {
  SingleLinkRelated link = new SingleLinkRelated();
  
  

  
  link.addNodeHead(1);link.addNodeHead(7);link.addNodeHead(5);link.addNodeHead(8);link.addNodeHead(3);link.addNodeHead(2);
  link.getAllInfo();
  Node newHead=link.linkMergeSort(link.getHead(), link.getSize());
  link.setHead(newHead);
  link.getAllInfo();
 }
 
 
 public Node linkMergeSort(Node head , int length){
  
  if(length==1)
   return head;
  else{
   int len1 = length/2;
   Node head1 = head;
   Node head2 = head;
   for(int i = 1;i< len1;i++){
    head2 = head2.next;
   }
   Node temp = head2;
   head2 = head2.next;
   temp.next = null;
   
   int len2 = length - len1;
   
   //归下去
   head1 = linkMergeSort(head1, len1);
   head2 = linkMergeSort(head2, len2);
   /////////////////////////////////////////////////////////////////
   //并起来
   Node c1;
   Node c2;
   if(head1.value<head2.value){
    head = head2;
    c1 = head1;
    c2 = head2.next;
   }
   else{
    head = head1;
    c1 = head1.next;
    c2 = head2;
   }
   Node current = head;
   while(c1!=null&&c2!=null){
    if(c1.value>=c2.value){
 
     current.next = c1;
     current =c1;
     c1 = c1.next;
     
    }
    else{
     current.next = c2;
     current =c2;
     c2 =c2.next;
    }
    
   }
   if(c1==null)
    current.next = c2;
   else {
    current.next = c1;
   }
   return head;
  }
 }
 
 

 
 public void addNodeHead(int info){
  
  Node node = new Node();
  node.value = info;
  
  if(head == null ){
   head = node;
   
  }
  else{
   node.next = head.next;
   head.next = node;
  }
  size++;
 }
 
 public void getAllInfo(){
  Node current = head;
  
  while(current!=null){
   System.out.print(current.value+" ");
   current = current.next;
  }
  System.out.println();
 }
}


class Node{
 //为省事都定义为public的
 public int value;
 public Node next;
 
 
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值