单链表排序

原创 2016年05月30日 20:16:37
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;
        }
    }
}
版权声明:

相关文章推荐

单链表逆序与排序

单链表的增删排序求长度的实现。

对于链表,我们可以知道他和算法会有相关。我们至少通过两组结构来存储:数组和链表。 接下来,我们来进行单链表的实现 在这里,我们可以进行增加,删除,求长度,排序。#define _CRT_SECUR...

第1章第2节练习题13 单链表之选择排序

有一个带头结点的单链表对其排序,使之递增有序

【leetcode】单链表的插入排序

单链表的直接插入排序排序思想跟数组的插入排序的思想是一样的。但是这里涉及到链表的插入删除等等,一些操作,实现起来还是稍微麻烦的。 链表的直接插入: 情况1:当前结点的值大于上一个结点的值,不用处理...

单链表排序

  • 2013-10-17 18:48
  • 40KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)