单链表的选择排序

题目描述

给定一个无序单链表,实现单链表的选择排序(按升序排序)。

示例1

输入

[1,3,2,4,5]

输出

{1,2,3,4,5}
/**
 * struct ListNode {
 *	int val;
 *	struct ListNode *next;
 * };
 */

class Solution {
public:
    /**
     * 
     * @param head ListNode类 the head node
     * @return ListNode类
     */
    ListNode* getSmallLeastPreNode(ListNode* head){
        ListNode* small = head;
        ListNode* smallPre = NULL;
        ListNode* cur = head->next;
        ListNode* pre = head;
        while(cur!=NULL){
            if(cur->val < small->val){
                smallPre = pre;
                small = cur;
            }
            pre = cur;
            cur = cur->next;
        }
        
        return smallPre;
    }
    ListNode* sortInList(ListNode* head) {
        // write code here
        ListNode* tail = NULL;
        ListNode* cur = head;
        ListNode* smallPre = NULL;
        ListNode* small = NULL;
        while(cur!=NULL){
            small = cur;
            smallPre = getSmallLeastPreNode(cur);
            if(smallPre!=NULL){
                small = smallPre->next;
                smallPre->next = small->next;
            }
            cur = cur==small?cur->next:cur;
            if(tail==NULL){
                head = small;
            }else{
                tail->next = small;
            }
            tail = small;
            
        }
        return head;
    }
};

 

好的,单链表选择排序的C语言实现可以分为以下几个步骤: 1. 定义链表节点结构体,包括数据域和指向下一个节点的指针域。 2. 定义链表排序函数,函数参数为链表头节点指针。 3. 在排序函数中,使用两个指针变量p和q,分别指向当前未排序部分的头节点和尾节点。 4. 在未排序部分中,找到最小值节点,并将其从未排序部分中删除。 5. 将最小值节点插入到已排序部分的尾部。 6. 重复步骤4和5,直到所有节点都被排序。 下面是单链表选择排序的C语言实现代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 typedef struct ListNode { int val; struct ListNode *next; } ListNode; // 定义链表排序函数 void selectionSortList(ListNode* head) { ListNode *p = head, *q = NULL, *minNode = NULL, *minPrev = NULL; while (p != NULL) { minNode = p; minPrev = NULL; q = p->next; while (q != NULL) { if (q->val < minNode->val) { minNode = q; minPrev = p; } p = p->next; q = q->next; } if (minNode != p) { if (minPrev != NULL) { minPrev->next = minNode->next; } else { head = minNode->next; } minNode->next = p; if (p == head) { head = minNode; } else { minPrev->next = minNode; } p = minNode; } else { p = p->next; } } } // 创建链表 ListNode* createList(int* nums, int numsSize) { ListNode *head = NULL, *tail = NULL; for (int i = 0; i < numsSize; i++) { ListNode *node = (ListNode*)malloc(sizeof(ListNode)); node->val = nums[i]; node->next = NULL; if (head == NULL) { head = node; tail = node; } else { tail->next = node; tail = node; } } return head; } // 打印链表 void printList(ListNode* head) { while (head != NULL) { printf("%d ", head->val); head = head->next; } printf("\n"); } int main() { int nums[] = {3, 1, 4, 2, 5}; int numsSize = sizeof(nums) / sizeof(int); ListNode *head = createList(nums, numsSize); printf("Before sorting: "); printList(head); selectionSortList(head); printf("After sorting: "); printList(head); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值