LeetCode //C - 147. Insertion Sort List

147. Insertion Sort List

Given the head of a singly linked list, sort the list using insertion sort, and return the sorted list’s head.

The steps of the insertion sort algorithm:

  1. Insertion sort iterates, consuming one input element each repetition and growing a sorted output list.
  2. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list and inserts it there.
  3. It repeats until no input elements remain.

The following is a graphical example of the insertion sort algorithm. The partially sorted list (black) initially contains only the first element in the list. One element (red) is removed from the input data and inserted in-place into the sorted list with each iteration.
在这里插入图片描述
 

Example 1:

在这里插入图片描述

Input: head = [4,2,1,3]
Output: [1,2,3,4]

Example 2:

在这里插入图片描述

Input: head = [-1,5,3,4,0]
Output: [-1,0,3,4,5]

Constraints:
  • The number of nodes in the list is in the range [1, 5000].
  • -5000 <= Node.val <= 5000

From: LeetCode
Link: 147. Insertion Sort List


Solution:

Ideas:

1. Node Definition and Creation:

  • The ListNode structure defines a node in a singly linked list.
  • The createNode function creates a new node with a given value.

2. Insertion Sort Function:

  • The insertionSortList function sorts the linked list using the insertion sort algorithm.
  • It iterates through each node in the original list, and inserts it into the correct position in the sorted list.

3. Helper Function:

  • The printList function is used to print the values of the linked list nodes.
Code:
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* createNode(int val) {
    struct ListNode* newNode = (struct ListNode*)malloc(sizeof(struct ListNode));
    newNode->val = val;
    newNode->next = NULL;
    return newNode;
}

struct ListNode* insertionSortList(struct ListNode* head) {
    if (!head) return NULL;
    
    struct ListNode* sorted = NULL;
    
    while (head) {
        struct ListNode* current = head;
        head = head->next;
        
        if (!sorted || sorted->val >= current->val) {
            current->next = sorted;
            sorted = current;
        } else {
            struct ListNode* temp = sorted;
            while (temp->next && temp->next->val < current->val) {
                temp = temp->next;
            }
            current->next = temp->next;
            temp->next = current;
        }
    }
    
    return sorted;
}

// Function to print the linked list
void printList(struct ListNode* head) {
    while (head) {
        printf("%d ", head->val);
        head = head->next;
    }
    printf("\n");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Navigator_Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值