insertion Sort List (链表的插入排序) leecode java

逻辑简单,代码难写,基础不劳,leecode写注释不能出现中文,太麻烦,我写了大量注释,链表问题最重要的就是你那个指针式干啥的

提交地址https://oj.leetcode.com/problems/insertion-sort-list/

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public ListNode insertionSortList(ListNode head) {
    if(head==null) return head;
    ListNode h=new ListNode(-1000000); //加入头结点方面,特别适合头结点不断改变的情况,c++要释放掉,java直接返回头的next就可
    h.next=head;
  ListNode cur=head.next; //cur 保存当前处理的节点
  head.next=null; //别忘了,新链表的尾巴值为空
    while(cur!=null)
    {
        ListNode nextNode=cur.next; //保存下一个要处理的点,因为cur会被插入的新链表中,所以保存,然后赋给cur(最后一句)
        
        ListNode l=h.next;
        ListNode pre=h;
        while(l!=null) //找到合适的插入位置,找pre地址
        {
            if(l.val<=cur.val)
            {
                pre=l;
                l=l.next;
            }
            else
            {
                break;
            }
        }
            
        
        //insert into the list
        cur.next=pre.next;
        pre.next=cur;
        
        
        
        
        cur=nextNode;
        
        
        
    }
    
    
         return h.next;
        
    }
   
    
    
    
}

 

转载于:https://www.cnblogs.com/hansongjiang/p/3814998.html

以下是C语言链表插入排序的完整代码实现: ```c #include <stdio.h> #include <stdlib.h> // 链表节点结构体 typedef struct Node { int data; struct Node* next; } Node; // 插入节点到链表Node* insertAtHead(Node* head, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->next = head; return newNode; } // 创建链表 Node* createList(int arr[], int n) { Node* head = NULL; for (int i = n - 1; i >= 0; i--) { head = insertAtHead(head, arr[i]); } return head; } // 打印链表 void printList(Node* head) { Node* p = head; while (p) { printf("%d ", p->data); p = p->next; } printf("\n"); } // 链表插入排序 Node* insertionSort(Node* head) { if (head == NULL || head->next == NULL) { return head; } Node* p = head->next; Node* newHead = head; newHead->next = NULL; while (p) { Node* q = p->next; if (p->data < newHead->data) { p->next = newHead; newHead = p; } else { Node* prev = newHead; Node* cur = newHead->next; while (cur && p->data > cur->data) { prev = cur; cur = cur->next; } prev->next = p; p->next = cur; } p = q; } return newHead; } int main() { int arr[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; int n = sizeof(arr) / sizeof(arr[0]); Node* head = createList(arr, n); printf("排序前:"); printList(head); head = insertionSort(head); printf("排序后:"); printList(head); return 0; } ``` 以上代码中,先定义了链表节点结构体 `Node`,包含一个整型数据 `data` 和一个指向下一个节点的指针 `next`。然后定义了插入节点到链表头的函数 `insertAtHead` 和创建链表的函数 `createList`,并用其中的 `insertAtHead` 函数来创建链表。接下来定义了打印链表的函数 `printList`。 最后,定义了链表插入排序的函数 `insertionSort`,该函数的参数是链表头节点指针,返回排好序后的新链表头节点指针。在 `main` 函数中,创建链表并打印未排序的链表,然后调用 `insertionSort` 函数进行排序,并打印排好序后的链表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值