LeetCode 148. 排序链表

在这里插入图片描述
在这里插入图片描述
官方

class Solution {
    public ListNode sortList(ListNode head) {
        return sortList(head, null);
    }

    public ListNode sortList(ListNode head, ListNode tail) {
        if (head == null) {
            return head;
        }
        if (head.next == tail) {
            head.next = null;
            return head;
        }
        ListNode slow = head, fast = head;
        while (fast != tail && fast.next != tail) {
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode mid = slow;
        ListNode list1 = sortList(head, mid);
        ListNode list2 = sortList(mid, tail);
        ListNode sorted = merge(list1, list2);
        return sorted;
    }

    public ListNode merge(ListNode head1, ListNode head2) {
        ListNode dummyHead = new ListNode(0);
        ListNode temp = dummyHead, temp1 = head1, temp2 = head2;
        while (temp1 != null && temp2 != null) {
            if (temp1.val <= temp2.val) {
                temp.next = temp1;
                temp1 = temp1.next;
            } else {
                temp.next = temp2;
                temp2 = temp2.next;
            }
            temp = temp.next;
        }
        if (temp1 != null) {
            temp.next = temp1;
        } else if (temp2 != null) {
            temp.next = temp2;
        }
        return dummyHead.next;
    }
}

快速排序链表

//写法一:值交换  5%
class Solution {
    public ListNode sortList(ListNode head) {
        quickSort(head, null);
        return head;
    }
    
    void quickSort(ListNode head, ListNode tail){
        if(head == tail || head.next == tail) return;
        int pivot = head.val;
        ListNode left = head, cur = head.next;
        
        while(cur != tail){
            if(cur.val < pivot){
                left = left.next;
                swap(left, cur);
            }
            cur = cur.next;
        }
        swap(head, left);
        quickSort(head, left);
        quickSort(left.next, tail);
    }
    public void swap(ListNode head, ListNode left){
        int tmp = head.val;
        head.val = left.val;
        left.val = tmp;
    }
}
//写法二:指针交换  会超时
// 不交换节点的值
class Solution {
    public ListNode sortList(ListNode head) {
		ListNode newList = partition(head);
        return newList;
    }
    public ListNode partition(ListNode head) {
        if (head == null || head.next == null) {
			return head;
		}
        ListNode left = new ListNode(0), ll = left;
        ListNode right = new ListNode(0), rr = right;
        ListNode base = head;
        int baseVal = base.val;
        ListNode cur = head.next;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = null;
            if (cur.val < baseVal) {
                ll.next = cur;
                ll = ll.next; 
            } else {
                rr.next =  cur;
                rr = rr.next;
            }
            cur = next;
        }
        left.next = partition(left.next);
        right.next = partition(right.next);
        
        cur = left;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = base;
        base.next = right.next;
        
        return left.next;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水之积也不厚,则其负大舟也无力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值