[C++]LeetCode: 126 Insertion Sort List (插入排序链表)

题目:Sort a linked list using insertion sort.

思路:题目要求我们用插入排序来实现链表排序。我们构建一个当前排好序的链表,然后维护一个变量,不断指向链表中的下一个节点。用变量cur表示当前要插入的节点,每次循环找到cur节点在当前排好序的链表中对应的位置,然后插入进去,然后指向原链表中下一个节点,继续进行插入过程,直到原链表的所有节点都完成,既经过N次迭代后,就得到了排序好的结果。

复杂度:时间O(N^2) 空间O(1)

Attention:

1. 维护一个当前排序好的链表,构建一个虚拟头结点。我们总是将原链表中的下一个节点,插入到新链表中的合适位置。

 ListNode* dummyhead = new ListNode(0);
 ListNode* pre = dummyhead;
//在当前排好的新链表中找到第一个大于cur->val的结点
            while(pre->next != NULL && pre->next->val <= cur->val)
            {
                pre = pre->next;
            }

            //当前pre的next结点的值大于cur的值,将cur插入到pre后
            cur->next = pre->next;
            pre->next = cur;

2. 维护一个节点next,表示原链表中的下一个要插入的节点。经过一轮迭代后,我们将cur 置为next. 这样我们就不用根据长度来判断下一个节点了。

ListNode* next = cur->next;  //维护链表的下一个结点
cur = next;   //cur指向原链表的下一个节点

3. 每次我们都需要从排序好的新链表的表头重新循环,找到要插入节点的合适位置。

pre = dummyhead;             //重置pre为新链表头开始

AC Code:

/**
 * 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* dummyhead = new ListNode(0);
        ListNode* pre = dummyhead;
        ListNode* cur = head;

        while(cur != NULL)
        {
            ListNode* next = cur->next;  //维护链表的下一个结点
            pre = dummyhead;             //重置pre为新链表头开始

            //在当前排好的新链表中找到第一个大于cur->val的结点
            while(pre->next != NULL && pre->next->val <= cur->val)
            {
                pre = pre->next;
            }

            //当前pre的next结点的值大于cur的值,将cur插入到pre后
            cur->next = pre->next;
            pre->next = cur;
            cur = next;   //cur指向原链表的下一个节点
        }
        return dummyhead->next;
    }
};

这道题,刚开始我也有考虑在原来的链表中进行插入操作,就和数组的操作有点像,因为我们要将当前元素插入已经排好序的链表的合适位置上,这样我们不仅需要比较大小,还需要保证pre节点不能跨过当前节点,这样就无法保证我们的插入时正确的。如{3,4,1}, 第一次迭代pre->next停在3, cur在4,我们并不能将4插到3前面。所以对于链表,在原链表的操作是比较复杂的。需要新建一个排序好的链表进行插入操作,可以避免很多判断。

时间: 2024-08-01 19:21:02

[C++]LeetCode: 126 Insertion Sort List (插入排序链表)的相关文章

Leetcode 147. Insertion Sort List 插入排序 in Java

147. Insertion Sort List Total Accepted: 80869 Total Submissions: 263074 Difficulty: Medium Sort a linked list using insertion sort. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { va

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 题解]: 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][JAVA] Insertion Sort List

Sort a linked list using insertion sort. 简单插入排序,先写个插入一个值到链表里的函数,再遍历整个链表,一个一个把值插入新链表中: 1 public ListNode insertionSortList(ListNode head) { 2 if(head==null) 3 return null; 4 ListNode re = new ListNode(head.val); 5 ListNode cur = head.next; 6 while(cur

Sort List &amp;&amp; Insertion Sort List (链表排序总结)

Sort List Sort a linked list in O(n log n) time using constant space complexity. Have you been asked this question in an interview?                   Yes               说明:归并排序: 时间 O(nlogn),空间 O(1). 每次将链表一分为二, 然后再合并.快排(用两个指针) /** * Definition for sing

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

[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,防止链表断开. /** * Definitio

leetcode 147. Insertion Sort List (Python版)

题目: Sort a linked list using insertion sort. 大意是要实现一个链表的插入排序 算法思路:     从原链表中逐个弹出一个node 对于每一个node用插入排序的思想插入新的升序排列的链表中 这里有一个小trick,leetcode有一组数据是1~4999的升序序列,如果我们采用如上方法会超时 于是我们在插入排序的时候设置一个last位,记录当前插入的位置 在下一次插入的时候与上次插入位置last比较,如果当前node.val > last.val,那么