关闭

单链表排序

标签: 单链表排序
69人阅读 评论(0) 收藏 举报
分类:
import java.util.ArrayList;
import java.util.List;

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

public class Solution {
    //快速排序
    public ListNode sortList(ListNode head){
        if(head == null){
            return null;
        }

        ListNode pivot = head;
        head = head.next;
        if(head == null){
            pivot.next = null;
            return pivot;
        }

        ListNode leftHead = new ListNode(0);
        ListNode rightHead = new ListNode(0);
        ListNode leftTail = leftHead;
        ListNode pivotTail = pivot;
        ListNode rightTail = rightHead;

        while (head != null){
            if(head.val < pivot.val){
                leftTail.next = head;
                leftTail = leftTail.next;
            }else if(head.val == pivot.val){
                pivotTail.next = head;
                pivotTail = pivotTail.next;
            }else {
                rightTail.next = head;
                rightTail = rightTail.next;
            }
            head = head.next;
        }

        leftTail.next = null;
        pivotTail.next = null;
        rightTail.next = null;

        ListNode leftReturnHead = sortList(leftHead.next);

        if(leftReturnHead == null){
            leftReturnHead = pivot;
        }else {
            ListNode connectLeftRight = leftReturnHead;
            while (connectLeftRight.next != null){
                connectLeftRight = connectLeftRight.next;
            }
            connectLeftRight.next = pivot;
        }

        pivotTail.next = sortList(rightHead.next);
        return leftReturnHead;
    }

    //归并排序
    public ListNode mergeTwoSortedList(ListNode leftHead, ListNode rightHead){
        ListNode result = new ListNode(0);
        ListNode resultHead = result;
        while (leftHead != null && rightHead != null){
            if (leftHead.val < rightHead.val){
                result.next = leftHead;
                leftHead = leftHead.next;
            }else {
                result.next = rightHead;
                rightHead = rightHead.next;
            }
            result = result.next;
        }

        if(leftHead != null){
            result.next = leftHead;
        }else {
            result.next = rightHead;
        }

        return resultHead.next;
    }

    public ListNode sortList1(ListNode head){
        if(head == null || head.next == null){
            return head;
        }

        ListNode slowPointer = head;
        ListNode fastPointer = head;
        ListNode prePointer = head;
        while (fastPointer != null && fastPointer.next != null){
            prePointer = slowPointer;
            slowPointer = slowPointer.next;
            fastPointer = fastPointer.next.next;
        }

        prePointer.next = null;
        ListNode leftHead = sortList1(head);
        ListNode rightHead = sortList1(slowPointer);

        return mergeTwoSortedList(leftHead, rightHead);
    }


    //转换为数组处理
    public ListNode sortList2(ListNode head){
        if(head == null || head.next == null){
            return head;
        }

        List<ListNode> arrayList = new ArrayList<>();

        while (head != null){
            arrayList.add(head);
            head = head.next;
        }

        ListNode result = new ListNode(0);
        ListNode resultHead = result;
        arrayList.sort((node1,node2)->Integer.compare(node1.val, node2.val));
        for(ListNode node : arrayList){
            result.next = node;
            result = result.next;
        }
        result.next = null;
        return resultHead.next;
    }

    public static void main(String[] args){
        ListNode head = new ListNode(3);
        ListNode one = new ListNode(3);
        ListNode two = new ListNode(1);
        ListNode three = new ListNode(2);
        head.next = one;
        one.next = two;
        two.next = three;
        three.next = null;
        ListNode re = new Solution().sortList(head);
        while (re != null){
            System.out.print(re.val + " ");
            re = re.next;
        }
    }
}
0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9879次
    • 积分:883
    • 等级:
    • 排名:千里之外
    • 原创:81篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类