单链表排序

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;
        }
    }
}
阅读更多
文章标签: 单链表 排序
个人分类: 编程学习
上一篇求数组中不重复的两个数
下一篇01背包问题
想对作者说点什么? 我来说一句

单链表实现从小到大排序

2010年05月24日 2KB 下载

没有更多推荐了,返回首页

关闭
关闭