[LeetCode]Sort List

本文详细介绍了如何使用归并排序算法对链表进行排序,同时保持常数空间复杂度,并确保时间复杂度为O(n log n)。通过逐步拆分、排序链表部分和合并排序后的部分,实现高效排序过程。本文还提供了Java和C++代码实现,帮助读者理解并应用这一技术。

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


O(n log n) complexity are merge sort and quick sort. So in this problem, we can use merge sort to handle!

Different from array data, this problem requires good understanding of linked list operations, e.g., split, merge.  Before doing this problem, I suggest you first check out the problem "Merge Two Sorted List" and "Linked List Cycle", and also the merge sort algorithm itself. Then the problem becomes pretty straightforward.

Like the merge sort, we can do recursively:
(1) Split the list (slow fast pointers)
(2) sort the first part (merge sort)
(3) sort the second part (merge sort)
(4) merge the two parts (merge two sorted lists)

java

public ListNode sortList(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode middleNode = getMiddleNode(head);
        ListNode next = middleNode.next;
        middleNode.next = null;
        return mergeTwoLists(sortList(head), sortList(next));
    }
	public ListNode getMiddleNode(ListNode head){
		ListNode slow = head;
        ListNode fast = head;
        while(fast.next!=null&& fast.next.next!=null){
        	slow = slow.next;
        	fast = fast.next.next;
        }
        return slow;
	}
	public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode temp = new ListNode(0);
        ListNode prev = temp;
        while(l1!=null && l2!=null){
        	if(l1.val>l2.val){
        		prev.next = l2;
        		l2 = l2.next;
        	}
        	else{
        		prev.next = l1;
        		l1 = l1.next;
        	}
        	prev = prev.next;
        }
        if(l1!=null) 
        	prev.next = l1;
        else if(l2!=null)
        	prev.next = l2;
        return temp.next;
    }

c++

ListNode *sortList(ListNode *head) {
        if(head == NULL || head->next == NULL) return head;
    int len = 1;
    ListNode *p = head;
    while(p->next != NULL){
        len++;
        p = p->next;
    }
    ListNode *newHead = Sort(&head, len);
    }
    ListNode *Sort(ListNode **head, int lenght){
    if(lenght == 1){
        ListNode *temp = *head;
        *head = (*head)->next;
        temp->next = NULL;
        return temp;
    }
    ListNode *leftHead = Sort(head,lenght/2);
    ListNode *rightHead = Sort(head,lenght-lenght/2);
    ListNode *newHead = merge2Lists(leftHead,rightHead);
    return newHead;
}
ListNode *merge2Lists(ListNode *head1, ListNode *head2){
        ListNode *head = new ListNode(INT_MIN);
        ListNode *pre = head;
        while(head1 != NULL && head2 != NULL){
            if(head1->val <= head2->val){
                pre->next = head1;
                head1 = head1->next;
            }else{
                pre->next = head2;
                head2 = head2->next;
            }
            pre = pre->next;
        }
        if(head1 != NULL){
            pre->next = head1;
        }
        if(head2 != NULL){
            pre->next = head2;
        }

        return head->next;
    }



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值