zoukankan      html  css  js  c++  java
  • leetcode linkedList sort

    链表排序:算法-归并排序
    
    public class LinkedSort {
    
        private static class ListNode {
            int val;
            ListNode next;
    
            ListNode(int x) {
                val = x;
                next = null;
            }
        }
    
        private ListNode mergeList(ListNode head1,ListNode head2){
            if(head1==null){
                return head2;
            }else if(head2==null){
                return head1;
            }
            ListNode head=new ListNode(0);
            ListNode tmp=head;
            while(head1!=null&&head2!=null){
                if(head1.val<=head2.val){
                    head.next=head1;
                    head1=head1.next;
                }else{
                    head.next=head2;
                    head2=head2.next;
                }
                head=head.next;
            }
            while(head1!=null){
                head.next=head1;
                head1=head1.next;
                head=head.next;
            }
            while(head2!=null){
                head.next=head2;
                head2=head2.next;
                head=head.next;
            }
            return tmp.next;//注意另起一个新的节点
        }
        public ListNode sortList(ListNode head) {
            if(head==null||head.next==null){
                return head;
            }
            ListNode first=head;
            ListNode after=head;
            // two nodes要特别处理是两个节点的情况
            if(head.next.next==null){
                first=head.next;
                head.next=null;
                return mergeList(head, first);
            }
            
            while(after!=null){
                if(after.next!=null){
                    after=after.next.next;
                    first=first.next;
                }else{
                    break;
                }
            }
            ListNode tmp=first.next;
            first.next=null;//这里容易出现问题,提前将后置节点归null
            ListNode head1=sortList(head);
            ListNode head2=sortList(tmp);
            return mergeList(head1, head2);
        }
        public static void main(String[] args){
            LinkedSort linkedSort=new LinkedSort();
            ListNode head1=new ListNode(9);
            head1.next=new ListNode(3);
            head1.next.next=new ListNode(4);
            head1.next.next.next=new ListNode(1);
            head1.next.next.next.next=new ListNode(5);
            ListNode head=linkedSort.sortList(head1);
            System.out.println("fuck");
            while(head!=null){
                System.out.println(head.val);
                head=head.next;
            }
            
        }
    }
  • 相关阅读:
    Java并发编程(二)线程任务的中断(interrupt)
    Java并发编程(一) 两种实现多线程的方法(Thread,Runnable)
    青蛙跳台阶(Fibonacci数列)
    旋转数组的最小值
    用两个栈实现队列
    重建二叉树
    二维数组中的查找
    Lab 3-1
    Lab 1-4
    Lab 1-3
  • 原文地址:https://www.cnblogs.com/csxf/p/3890230.html
Copyright © 2011-2022 走看看