mirror of
				https://git.wolves.top/wolves/leetcode.git
				synced 2025-11-04 17:26:32 +08:00 
			
		
		
		
	init
This commit is contained in:
		
							
								
								
									
										36
									
								
								we/24-sy-2.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								we/24-sy-2.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
struct ListNode
 | 
			
		||||
{
 | 
			
		||||
    int val;
 | 
			
		||||
    struct ListNode *next;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
// 带头节点的单链表插入排序
 | 
			
		||||
void insertSort(struct ListNode *head){
 | 
			
		||||
    struct ListNode *p = head->next;
 | 
			
		||||
    struct ListNode *q;
 | 
			
		||||
    
 | 
			
		||||
    if (p == NULL || p->next == NULL) return; // 空链表或单节点无需排序
 | 
			
		||||
 | 
			
		||||
    while(p->next) {
 | 
			
		||||
        q = head;
 | 
			
		||||
        while (q->next != p->next) {
 | 
			
		||||
            if(q->next->val < p->next->val) {
 | 
			
		||||
                q = q->next;
 | 
			
		||||
            } else {
 | 
			
		||||
                // 插入操作
 | 
			
		||||
                struct ListNode *temp = p->next;
 | 
			
		||||
                p->next = temp->next;
 | 
			
		||||
                temp->next = q->next;
 | 
			
		||||
                q->next = temp;
 | 
			
		||||
                break; // 插入完成后立即跳出
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        // 只有没发生插入时才移动p指针
 | 
			
		||||
        if (q->next == p->next) { 
 | 
			
		||||
            p = p->next;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user