【Leetcode】Sort List (Sorting)

此题要求用归并排序排两个链表,基本思想还是分为分割和合并

合并的代码在Merge Two Sorted List里已经讲得很清楚了。所以这里直接给出代码。

	public ListNode merge(ListNode l1, ListNode l2) {
		ListNode helper = new ListNode(0);
		ListNode runner = helper;
		while (l1 != null && l2 != null) {
			if (l1.val < l2.val) {
				runner.next = l1;
				l1 = l1.next;
				runner = runner.next;
			} else {
				runner.next = l2;
				l2 = l2.next;
				runner = runner.next;
			}
			if (l1 != null)
				runner.next = l1;
			if (l2 != null)
				runner.next = l2;
		}
		return helper.next;
	}

分割的思想很重要,在数组里面是通过分割知道只剩下一个元素的时候结束,链表也一样。

数组中判断只有一个元素的方法是if(left==right)

链表中判断只有一个元素的方法是if(head.next==null)

数组中的分割的方法是

		int middle = (left + right) / 2;
		mergeSort(array, left, middle);
		mergeSort(array, middle + 1, right);
		merge(array, left, middle, right);

链表中怎么找middle呢?这个时候我们想起了前面所提到的Walker_Runner技术来找中点。

		ListNode walker = head;
		ListNode runner = head;
		while (runner.next != null && runner.next.next != null) {
			walker = walker.next;
			runner = runner.next.next;
		}
		ListNode head2 = walker.next;
		walker.next = null;
		head = mergeSort(head);
		head2 = mergeSort(head2);
		return merge(head, head2);

下面给出完整的代码

	public ListNode sortList(ListNode head) {
		if (head == null)
			return head;
		return mergeSort(head);
	}

	public ListNode mergeSort(ListNode head) {
		if (head.next == null)
			return head;
		ListNode walker = head;
		ListNode runner = head;
		while (runner.next != null && runner.next.next != null) {
			walker = walker.next;
			runner = runner.next.next;
		}
		ListNode head2 = walker.next;
		walker.next = null;
		head = mergeSort(head);
		head2 = mergeSort(head2);
		return merge(head, head2);
	}

	public ListNode merge(ListNode l1, ListNode l2) {
		ListNode helper = new ListNode(0);
		ListNode runner = helper;
		while (l1 != null && l2 != null) {
			if (l1.val < l2.val) {
				runner.next = l1;
				l1 = l1.next;
				runner = runner.next;
			} else {
				runner.next = l2;
				l2 = l2.next;
				runner = runner.next;
			}
			if (l1 != null)
				runner.next = l1;
			if (l2 != null)
				runner.next = l2;
		}
		return helper.next;
	}
时间: 2024-10-27 11:26:37

【Leetcode】Sort List (Sorting)的相关文章

【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】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, a

【LeetCode】 sort list 单链表的归并排序

题目:Sort a linked list in O(n log n) time using constant space complexity. 思路:要求时间复杂度O(nlogn) 知识点:归并排序,链表找到中点的方法 存在的缺点:边界条件多考虑!!! /** * LeetCode Sort List Sort a linked list in O(n log n) time using constant space complexity. * 题目:将一个单链表进行排序,时间复杂度要求为o

【Leetcode】Sort List JAVA实现

Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代码实现 package com.edu.leetcode; import com.edu.leetcode.ListNode; public class SortList { /** * @param args */ public ListNode Merge(ListNode first,List

【LeetCode】Sort Colors 数组排序

题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors 题目:输入一个数组,包含0,1,2分别代表红白蓝三种颜色,要求按照0,1,2的顺序,将同类颜色的连续排列 思路:计数排序,是一个遍历两遍的方法:可以先统计每种的数量,之后直接将这一范围内的所有值都赋值为相应的数字即可 遍历一遍的话可以在遍历的同时分别与0和2比较,从头和尾一起交换,1的在中间不用做处理: * */ package javaTr

【Leetcode】Sort List in java,你绝对想不到我是怎么做的^^我写完过了我自己都觉得好jian~

Sort a linked list in O(n log n) time using constant space complexity. 大家看完题目估计跟我一样啦...都在想哪些是nlogn啊~快速排序.归并排序.堆排序!然后开始愁,这些东西变成list了可怎么办啊... 可是我深深地记得在CMU的时候老师告诉我,java现在自带的Arrays.sort用的是快排,然后我就想,那么--为什么不把list转成array然后排完了放回去呢! 于是我就用一个arraylist先装这个list,然

【LeetCode】Sort List

题目 Sort a linked list in O(n log n) time using constant space complexity. 解答 O(nlogn)时间复杂度的排序有快排.堆排.归并,一般双向链表用快排.单向链表用归并,堆排两种都可以,以下使用归并排序: /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * v

【leetcode】Sort Colors(middle)☆

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】Sort List解答

一.原题 Sort List Sort a linked list in O(n log n) time using constant space complexity. 二.分析 快速排序和归并排序的时间复杂度是O(nlogn).如果使用归并排序,在使用链表的情况下,不需要重新申请空间存放排序后的数组,可以做到空间复杂度数O(1).我在这里使用归并排序来排序链表. 三.代码(java) <pre name="code" class="java">/**