148. Sort List

Sort a linked list in O(n log n) time using constant space complexity.

java

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode sortList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode leftDummy = new ListNode(0);
        ListNode midDummy = new ListNode(0);
        ListNode rightDummy = new ListNode(0);
        ListNode left = leftDummy;
        ListNode mid = midDummy;
        ListNode right = rightDummy;
        ListNode node = getMid(head);
        while (head != null) {
            if (head.val < node.val) {
                left.next = head;
                left = left.next;
            } else if (head.val > node.val) {
                right.next = head;
                right = right.next;
            } else {
                mid.next = head;
                mid = mid.next;
            }
            head = head.next;
        }
        left.next = null;
        mid.next = null;
        right.next = null;
        ListNode start = sortList(leftDummy.next);
        ListNode end = sortList(rightDummy.next);
        ListNode result = combine(start, midDummy.next, end);
        return result;
    }
    private ListNode getMid(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        while (fast != null && fast.next != null) {
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
    private ListNode combine(ListNode head, ListNode mid, ListNode end) {
        ListNode dummy = new ListNode(0);
        ListNode pre = dummy;
        if (head != null) {
            pre.next = head;
        }
        pre = getTail(pre);
        if (mid != null) {
            pre.next = mid;
        }
        pre = getTail(pre);
        if (end != null) {
            pre.next = end;
        }
        return dummy.next;
    }
    private ListNode getTail(ListNode head) {
        if (head == null) {
            return head;
        }
        while (head.next != null) {
            head = head.next;
        }
        return head;
    }
}

python

"""
Definition of ListNode
class ListNode(object):

    def __init__(self, val, next=None):
        self.val = val
        self.next = next
"""


class Solution:
    """
    @param: head: The head of linked list.
    @return: You should return the head of the sorted linked list, using constant space complexity.
    """
    def sortList(self, head):
        # write your code here
        if head == None or head.next == None:
            return head
        leftDummy, midDummy, rightDummy = ListNode(0), ListNode(0), ListNode(0)
        left, mid, right = leftDummy, midDummy, rightDummy
        node = self.getMid(head)
        while head is not None:
            if head.val < node.val:
                left.next = head
                left = left.next
            elif head.val > node.val:
                right.next = head
                right = right.next
            else:
                mid.next = head
                mid = mid.next
            head = head.next
        left.next, right.next, mid.next = None, None, None
        start = self.sortList(leftDummy.next)
        end = self.sortList(rightDummy.next)
        result = self.combine(start, midDummy.next, end)
        return result
    
    def getMid(self, head):
        if head == None or head.next == None:
            return head
        slow, fast = head, head.next
        while fast != None and fast.next != None:
            slow = slow.next
            fast = fast.next.next
        return slow
    
    def combine(self, head, mid, end):
        dummy = ListNode(0)
        pre = dummy
        if head is not None:
            pre.next = head
        pre = self.getTail(pre)
        if mid is not None:
            pre.next = mid
        pre = self.getTail(pre)
        if end is not None:
            pre.next = end
        return dummy.next
    
    def getTail(self, head):
        if head == None or head.next == None:
            return head
        while head.next != None:
            head = head.next
        return head


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ncst

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

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

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

打赏作者

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

抵扣说明:

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

余额充值