[LeetCode#148]Sort List

The problem:

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

My analysis:

The idea behind this problem is easy : merge sort !
But we should learn some tricky skills from this question.
1. How to split a linked list into two separate linked lists?
a. use two pointers: walker and runner.
a.1 walker move one step in each iteration.
a.2 runner move two steps in each iteration.
b. iterate until runner can‘t move any more (runner.next == null || runner.next.next == null)
c. then head1 is the head of original list, and head2 is the next element of runner.

2. How to merge two linked list in an elegant way?
The key idea behind writing an elegant code is always use the same invarint.
To achieve this purpose, I use a dummy head as fake head and a pre pinter always point to the last element of the resulting list.
ListNode dummy = new ListNode(0);
pre = dummy;
...
while () {
pre.next = ...;
}
return dummy.next;

My code:

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode sortList(ListNode head) {
        return mergeSort(head);
    }

    private ListNode mergeSort(ListNode head) {
        if (head == null || head.next == null) //the base case in the recursion is very important!
            return head;

        ListNode walker = head;
        ListNode runner = head;

        while (runner.next != null && runner.next.next != null) { //skill: check runner.next at first. 

            walker = walker.next; //this skill is amazing!!!
            runner = runner.next.next;
        }

        ListNode head2 = walker.next;
        walker.next = null;
        ListNode head1 = head;

        head1 = mergeSort(head1);
        head2 = mergeSort(head2);
        head = merge(head1, head2);

        return head;
    }

    private ListNode merge(ListNode head1, ListNode head2) {

        if (head1 == null && head2 == null)
            return null;

        if (head1 == null && head2 != null)
            return head2;

        if (head1 != null && head2 == null)
            return head1;

        ListNode dummy = new ListNode(0);
        ListNode pre = dummy;

        ListNode ptr1 = head1;
        ListNode ptr2 = head2;

        while (ptr1 != null && ptr2 != null) {

            if (ptr1.val <= ptr2.val) {

                pre.next = ptr1;
                pre = pre.next;
                ptr1 = ptr1.next;
            } else {

                pre.next = ptr2;
                pre = pre.next;
                ptr2 = ptr2.next;
            }
        }

        if (ptr1 == null)
            pre.next = ptr2;
        else
            pre.next = ptr1;

        return dummy.next;
    }
}
时间: 2024-10-18 02:15:55

[LeetCode#148]Sort List的相关文章

Leetcode 148. Sort List 归并排序 in Java

148. Sort List Total Accepted: 81218 Total Submissions: 309907 Difficulty: Medium Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; *

[Lintcode]98. Sort List/[Leetcode]148. Sort List

98. Sort List/148. Sort List 本题难度: Medium Topic: Linked List Description Sort a linked list in O(n log n) time using constant space complexity. Example Example 1: Input: 1->3->2->null Output: 1->2->3->null Example 2: Input: 1->7->2

[LeetCode] 148. Sort List 解题思路

Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn),额外空间为 O(1). O(n*logn) 时间排序算法,无法是 quick sort, merge sort, head sort.quick sort 需要灵活访问前后元素,适合于数组,merge sort 只需要从左到右扫过去即可,可用于列表结构. 当列表元素个数大于2时,将列表拆分为左右

Java for LeetCode 148 Sort List

Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 归并排序.快速排序.堆排序都是O(n log n),由于优先级队列是用堆排序实现的,因此,我们使用优先级队列即可,JAVA实现如下: public ListNode sortList(ListNode head) { if(head==null||head.next==null) return head; Queue<ListNode> pQ

leetcode 148. Sort List ----- java

Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复杂度,那么就使用归并就可以了. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ p

[LeetCode] 148. Sort List 链表排序

Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2->1->3 Output: 1->2->3->4 Example 2: Input: -1->5->3->4->0 Output: -1->0->3->4->5 解法:归并排序.由于有时间和空间复杂度的要求.把链表从中间分开,递归下去,都

[LeetCode]148. Sort List链表归并排序

要求时间复杂度O(nlogn),空间复杂度O(1),采用归并排序 传统的归并排序空间复杂度是O(n),原因是要用一个数组表示合并后的数组,但是这里用链表表示有序链表合并后的链表,由于链表空间复杂度是O(1),所以可以. 链表问题经常出现TLE问题或者MLE问题,这时候要检查链表拼接过程或者循环过程,看有没有死循环 public ListNode sortList(ListNode head) { if (head==null||head.next==null) return head; //利用

LeetCode Insertion Sort List

class Solution { public: ListNode *insertionSortList(ListNode *head) { if (head == NULL) return NULL; ListNode* sorted_head = head; ListNode* unsorted_head = head->next; head->next = NULL; ListNode* cur = unsorted_head; while (cur != NULL) { unsorte

【LeetCode】Sort Colors

LeetCode OJ Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, w