【leetcode刷题笔记】Insertion Sort List

Sort a linked list using insertion sort.



题解:实现链表的插入排序。

要注意的地方就是,处理链表插入的时候尽量往当前游标的后面插入,而不要往前面插入,后者非常麻烦。所以每次利用kepeler.next.val和head.val比较大小,而不是kepeler.val和head.val比较大小,因为如果用后者,要把head指向的节点插入到kepeler指向的节点的前面,如果kepeler指向的节点是头结点,就更麻烦了。

代码如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode insertionSortList(ListNode head) {
14         ListNode newHead = new ListNode(0);
15
16           while(head != null){
17               ListNode kepeler = newHead;
18               while(kepeler.next != null && kepeler.next.val < head.val){
19                   kepeler = kepeler.next;
20               }
21
22               ListNode temp = head.next;
23               head.next = kepeler.next;
24               kepeler.next = head;
25               head = temp;
26           }
27
28           return newHead.next;
29     }
30 }

【leetcode刷题笔记】Insertion Sort List,布布扣,bubuko.com

时间: 2024-08-05 07:45:36

【leetcode刷题笔记】Insertion Sort List的相关文章

【leetcode刷题笔记】Sort List

Sort a linked list in O(n log n) time using constant space complexity. 题解:实现一个链表的归并排序即可.主要分为三部分: 1.找到中点并返回的函数findMiddle; 2.归并函数merge; 3.排序函数sortList. 数组的findMiddle函数非常容易实现,链表就有一点tricky了.首先设置两个指针,一个slow初始化为head,一个fast初始化为head.next,然后slow一次走一步,fast一次走两

【leetcode刷题笔记】4Sum

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending order.

【leetcode刷题笔记】Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1]. 题解:跟Permutation差不多,只是这次会有重复的元素,如下图所示,如果只用DFS的话就会产生重复的排列: 上

【leetcode刷题笔记】Merge Intervals

Given a collection of intervals, merge all overlapping intervals. For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18]. 题解:首先对所有的区间按照start大小排序,然后遍历排序后的数组,用last记录前一个区间,如果遍历的当前区间可以和last合并,就把它合并到last里面:否则就把last放到answer list中,并且更新last

【leetcode刷题笔记】Subsets

Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example,If S = [1,2,3], a solution is: [ [3], [1], [2], [1,2,3], [1

【leetcode刷题笔记】Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题解: 所谓的anagrams,只若干个词,它们包含的字母的个数和种类完全一样,只是字符的顺序不一样.比如说bus,usb,sub就是一组angrams.同一组angrams具有排序后相同的特点,比如对上述三个单词按字典序排序分别得到bsu,bsu,bsu.我们用这一点

【leetcode刷题笔记】3Sum

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The solut

【leetcode刷题笔记】Scramble String

Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively. Below is one possible representation of s1 = "great": great / gr eat / \ / g r e at / a t To scramble the string, we may choose a

【leetcode刷题笔记】Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / 2 3 T