148. Sort List(链表归并排序)

本文介绍了一种使用常数空间复杂度,在O(nlogn)时间内对链表进行排序的算法。通过递归将链表分为两半,然后分别对两半进行排序,最后将两个已排序的链表合并为一个有序链表。

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

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

class Solution {
public:
    ListNode *sortList(ListNode *head) {
        if(head == NULL || head->next == NULL) {
            return head;
        }
        ListNode * slow = head;
        ListNode * fast = head;
        while(slow->next != NULL && fast->next->next != NULL) {
            slow = slow->next;
            fast = fast->next->next;
        }
        ListNode * mid = slow->next;
        slow->next = NULL;
        head = sortList(head);
        mid = sortList(mid);
        return merge(head, mid);
    }
    ListNode *merge(ListNode *headone, ListNode *headtwo) {
        if(headone == NULL) 
            return headtwo;
        if(headtwo == NULL) 
            return headone;
        ListNode * headnew = new ListNode(-1);
        ListNode * last = headnew;
        while(headone != NULL && headtwo != NULL) {
            if(headone->val < headtwo->val) {
                last->next = headone;
                headone = headone->next;
            }else {
                last->next = headtwo;
                headtwo = headtwo->next;
            }
            last = last->next;
        }
        if(headone == NULL) {
            last->next = headtwo;
        }
        if(headtwo == NULL) {
            last->next = headone;
        }
        return headnew->next;
    }
};
链表归并排序是一种常见的排序算法,用于对链表进行排序。下面是一个 Python 实现的链表归并排序的示例代码: ```python class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def merge_sort(head): if not head or not head.next: return head # 找到链表中点 slow, fast = head, head.next while fast and fast.next: slow = slow.next fast = fast.next.next right_head = slow.next slow.next = None # 递归地对子链表进行归并排序 left_sorted = merge_sort(head) right_sorted = merge_sort(right_head) # 合并两个有序链表 dummy = ListNode(0) curr = dummy while left_sorted and right_sorted: if left_sorted.val < right_sorted.val: curr.next = left_sorted left_sorted = left_sorted.next else: curr.next = right_sorted right_sorted = right_sorted.next curr = curr.next if left_sorted: curr.next = left_sorted else: curr.next = right_sorted return dummy.next ``` 这个示例代码中,我们定义了一个 `ListNode` 类来表示链表节点。`merge_sort` 函数使用递归的方式实现了链表归并排序。首先找到链表的中点,然后将链表切分为两个子链表,分别对两个子链表进行归并排序,最后合并两个有序子链表并返回结果。 你可以使用以下代码测试上述实现: ```python # 创建链表 head = ListNode(4) node1 = ListNode(2) node2 = ListNode(1) node3 = ListNode(3) head.next = node1 node1.next = node2 node2.next = node3 # 打印排序前的链表 curr = head while curr: print(curr.val, end=" ") curr = curr.next # 对链表进行归并排序 sorted_head = merge_sort(head) # 打印排序后的链表 print("\nSorted list:") curr = sorted_head while curr: print(curr.val, end=" ") curr = curr.next ``` 以上代码首先创建了一个包含 4、2、1、3 的链表,然后对链表进行归并排序,并打印排序前后的链表结果。 希望能对你有所帮助!如果有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值