【Insertion Sorted List】cpp

题目:

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* insertionSortList(ListNode* head) {
            ListNode *p1 = head;
            ListNode dummy(INT_MIN);
            while (p1)
            {
                ListNode *tmp1 = p1->next;
                ListNode *p2 = &dummy;
                while ( p2->next )
                {
                    if ( p2->next->val > p1->val )
                    {
                        ListNode *tmp2 = p2->next;
                        p2->next = p1;
                        p1->next = tmp2;
                        break;
                    }
                    else
                    {
                        p2 = p2->next;
                    }
                }
                if (!p2->next)
                {
                    p2->next = p1;
                    p1->next = NULL;
                }
                p1 = tmp1;
            }
            return dummy.next;
    }
};

tips:

插入排序算法在链表上的实现。

1. 设立一个虚表头dummy,虚表头后面接的就是已经排序好的部分ListNodes

2. 维护一个指针p1,始终指向待插入的ListNode

3. 里层的while循环需要选择插入的具体位置

时间: 2024-12-21 13:28:32

【Insertion Sorted List】cpp的相关文章

【Merge Sorted Array】cpp

题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements i

【Pascal's Triangle】cpp

题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 代码: class Solution { public: vector<vector<int>> generate(int numRows) { vector<vector

【Remove Duplicates from Sorted List 】cpp

题目: 第一次刷的时候漏掉了这道题. Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 代码: /** * Definition for singly-linked lis

【Search In Rotated Sorted Array】cpp

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

【Remove Duplicates from Sorted Array】cpp

题目: https://leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do

【Merge K Sorted Lists】cpp

题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ cla

【Merge Two Sorted Lists】cpp

题目: Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode

leetcode 【 Merge Sorted Array 】python 实现

题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B ar

leetcode 【 Insertion Sort List 】 python 实现

题目: Sort a linked list using insertion sort. 代码:oj测试通过 Runtime: 860 ms 1 # Definition for singly-linked list. 2 # class ListNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.next = None 6 7 class Solution: 8 # @param head, a ListNode 9 # @re