leetcode之Sort List

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

这道题属于人生中第一次对链表进行操作,首先,不同于C++中的struct,java中可以用一个类来定义一个数据结构。

这道题一看没有任何思路,就看别人的代码,发现getmid是一个比较巧妙地得到链表中中间节点的方法,其次在主函数中用到了递归,这样每一次合并就相当于一个归并排序,二路归并。

最后在merge函数中,合并两个链表,比如说,递归的最里层,合并两个节点,很巧妙的将已经加入列表的列表的值设为Integer.MAX_VALUE;这样就可以将没有合并到有序列表中的另一个有序链表合并进去。

下面附上从网上找来的源码,继续学习啊,自己会的太少了

public static ListNode sortList(ListNode head) {
        if (head == null || head.next == null)
            return head;
        ListNode p = getMid(head);
        ListNode q;
        q = p.next;
        p.next = null;
        p = head;
        p = sortList(p);
        q = sortList(q);
        return merge(p, q);
    }

    public static ListNode getMid(ListNode head) {
        ListNode p = head;
        ListNode q = head;
        while (p.next != null && q.next != null && q.next.next != null) {
            p = p.next;
            q = q.next.next;
        }
        return p;

    }

    public static ListNode merge(ListNode p, ListNode q) {
        ListNode r = new ListNode(0);
        ListNode res = r;
        while (p != null || q != null) {
            int a,b;
            if (p == null)
                a = Integer.MAX_VALUE;
            else
                a = p.val;

            if (q == null)
                b = Integer.MAX_VALUE;
            else
                b = q.val;

            if (a < b) {
                r.next = p;
                p = p.next;
            }
            else {
                r.next = q;
                q = q.next;
            }
            r = r.next;
        }
        return res.next;
    }

	public static void main(String[] args){

		ListNode[] ln = new ListNode[8];
		ln[0] = new ListNode(2);
		ln[1] = new ListNode(4);
		ln[2] = new ListNode(5);
		ln[3] = new ListNode(6);
		ln[4] = new ListNode(3);
		ln[5] = new ListNode(8);
		ln[6] = new ListNode(10);
		ln[7] = new ListNode(1);
		ln[0].next = ln[1];
		ln[1].next = ln[2];
		ln[2].next = ln[3];
		ln[3].next = ln[4];
		ln[4].next = ln[5];
		ln[5].next = ln[6];
		ln[6].next = ln[7];
		ln[7].next = null;
		ListNode s = sortList(ln[0]);

		while(s!=null){
			System.out.println(s.val);
			s= s.next;

		}

	}
时间: 2024-10-23 12:11:12

leetcode之Sort List的相关文章

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

[leetcode]Insertion Sort List @ Python

原题地址:http://oj.leetcode.com/problems/insertion-sort-list/ 题意:对链表进行插入排序. 解题思路:首先来对插入排序有一个直观的认识,来自维基百科. 代码循环部分图示: 代码: class Solution: # @param head, a ListNode # @return a ListNode def insertionSortList(self, head): if not head: return head dummy = Lis

leetcode——Insertion Sort List 对链表进行插入排序(AC)

Sort a linked list using insertion sort. class Solution { public: ListNode *insertionSortList(ListNode *head) { if(head == NULL || head->next == NULL) return head; ListNode *result; result->val = INT_MIN; result->next = NULL; ListNode *cur=head,*

[LeetCode OJ] Sort Colors

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, white, and bl

LeetCode :: Insertion Sort List [详细分析]

Sort a linked list using insertion sort. 仍然是一个非常简洁的题目,让我们用插入排序给链表排序:这里说到插入排序,可以来回顾一下, 最基本的入门排序算法,就是插入排序了:时间复杂度为n^2,最基本的插入排序是基于数组实现的,下面给出基于数组实现的插入排序,来体会一个插入排序的思想: 以下仅为数组实现,不是解题代码,没兴趣可以跳过. void insertionsort (int a[], int N) { for (int i = 1; i < N; i+

leetcode 专题—sort

此将主要将leetcode中sort专题的解答都放在这里,后续会慢慢加入 一:leetcode179 Largest Number 题目: Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The re

LeetCode OJ - Sort List

题目: Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 复杂度为O(n* logn) 的排序算法有:快速排序.堆排序.归并排序.对于链表这种数据结构,使用归并排序比较靠谱.递归代码如下: 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNod

LeetCode: Insertion Sort List [147]

[题目] Sort a linked list using insertion sort. [题意] 用插入排序方法排序链表 [思路] 直接搞 [代码] /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode

leetcode linkedList sort

链表排序:算法-归并排序 public class LinkedSort { private static class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } } private ListNode mergeList(ListNode head1,ListNode head2){ if(head1==null){ return head2; }else if(head2==null)