[LeetCode]85. Insertion Sort List链表插入排序

Sort a linked list using insertion sort.

Subscribe to see which companies asked this question

解法:设置3个指针:应插入位置的前一个节点first、当前处理节点third和其前一个节点second,设置辅助节点help指向头节点。然后从头节点的next节点开始遍历,一个个插入正确位置即可。注意在插入到新位置之前需要链接好second和third->next,防止链表断开。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* insertionSortList(ListNode* head) {
        if (head == NULL || head->next == NULL) return head;
        ListNode* help = new ListNode(0);
        help->next = head;
        ListNode *second = head, *third = head->next;
        while (third != NULL) {
            ListNode* first = help;
            if (third->val < second->val) {
                while (first->next->val <= third->val) // 找到插入点
                    first = first->next;
                second->next = third->next; // 链接断开点
                third->next = first->next; // 插入到新的位置
                first->next = third;
                third = second->next; // 前移一个节点
            }
            else { // 前移一个节点
                second = third;
                third = third->next;
            }
        }
        return help->next;
    }
};
时间: 2024-10-29 20:01:22

[LeetCode]85. Insertion Sort List链表插入排序的相关文章

LeetCode Insertion Sort List 链表插入排序

题意:给一个链表,实现插入排序. 思路:O(1)空间,O(n*n)复杂度.将排好的用另一个链表头串起来,那个链表头最后删掉,再返回链表. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ListNode(int x) : val(x), next(NULL) {} 7 * }; 8 */ 9 class Solution { 10 pub

147 Insertion Sort List 链表插入排序

用插入排序对链表进行排序. 详见:https://leetcode.com/problems/insertion-sort-list/description/ /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListN

Insertion Sort List 链表插入排序

地址:https://oj.leetcode.com/problems/insertion-sort-list/ public class Solution { public ListNode insertionSortList(ListNode head) { if(head==null || head.next ==null){ return head; } ListNode ans = new ListNode(head.val); ListNode pre = null; ListNod

【数据结构】算法 LinkList (Insertion Sort List 链表插入排序)

将一个单链表进行处理后,所得结果为一有序链表 Solution: 将原始链表逐个查询,插入新链表,在插入的同时对链表进行排序.时间复杂度O(n*n) public ListNode insertionSortList(ListNode head) { ListNode dummy = new ListNode(0); while (head != null) { ListNode node = dummy; while (node.next != null && node.next.val

insertion Sort List (链表的插入排序) leecode java

逻辑简单,代码难写,基础不劳,leecode写注释不能出现中文,太麻烦,我写了大量注释,链表问题最重要的就是你那个指针式干啥的 提交地址https://oj.leetcode.com/problems/insertion-sort-list/ /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; *

[LeetCode 题解]: Insertion Sort List

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】Insertion Sort List

题目 Sort a linked list using insertion sort. 解答 链表无法像数组那样从后往前依次比较插入,只能从前往后:在链表首部添加一个哨兵可以稍微简化下代码,代码如下: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * }

leetcode - [5]Insertion Sort List

Sort a linked list using insertion sort. 思路:插入排序 #include <iostream> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x): val(x), next(NULL) {} }; class Solution { public: ListNode *insertionSortList(ListNode *head) { if

LeetCode OJ - Insertion Sort List

题目: Sort a linked list using insertion sort. 解题思路: 假设 list[1..i]是排好序的,找到第i+1个元素应该插入的位置及其前驱,然后将其插入. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class S