LintCode刷题链表排序——归并和快速排序

本文介绍了一种使用归并排序和快速排序对链表进行排序的方法。通过定义链表节点类,实现两种排序算法,并详细展示了如何利用这些算法高效地对链表元素进行排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

归并排序

/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */ 
public class Solution {
    /**
     * @param head: The head of linked list.
     * @return: You should return the head of the sorted linked list,
                    using constant space complexity.
     */
    public ListNode sortList(ListNode head) {  
        // write your code here
        if (head == null || head.next==null) return head;

        // QuickSort(head, null);
        head = MergeSort(head);
        return head;

    }

    public ListNode Merge(ListNode a, ListNode b) {

        if(a == null && b == null) return null;
        ListNode temp = new ListNode(0);
        ListNode p = temp;

        while(a!=null && b!=null) {
            if (a.val < b.val) {
                p.next = a;
                p = p.next;
                a = a.next;
            } else {
                p.next = b;
                p = p.next;
                b = b.next;
            }
        }

        if (a!=null) p.next = a;
        if (b!=null) p.next = b;
        return temp.next;
    }

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

        ListNode pSlow = head;
        ListNode pFast = head.next;

        while(pFast.next!=null && pFast.next.next!=null){
            pSlow = pSlow.next;
            pFast = pFast.next.next;
        }

        pFast = MergeSort(pSlow.next);
        pSlow.next = null;
        pSlow = MergeSort(head);
        return Merge(pSlow, pFast);

    }
}

快速排序

/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */ 
public class Solution {
    /**
     * @param head: The head of linked list.
     * @return: You should return the head of the sorted linked list,
                    using constant space complexity.
     */
    public ListNode sortList(ListNode head) {  
        // write your code here
        if (head == null || head.next==null) return head;        
        QuickSort(head, null);
        return head;

    }    

    public void QuickSort(ListNode start, ListNode end) {

        if (start == end) return;
        ListNode a = start, b = start.next;

        while(b!=end) {
            if (b.val < start.val) {
                a = a.next;
                exch(a,b);
            }
            b = b.next;
        }

        exch(a, start);
        QuickSort(start, a);
        QuickSort(a.next, end);
    }

    private void exch(ListNode a, ListNode b) {
        int temp = a.val;
        a.val = b.val;
        b.val = temp;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值