链表的插入排序 Insertion Sort List

问题:

Sort a linked list using insertion sort.

解决:

① 链表插入排序。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution { //46ms
    public ListNode insertionSortList(ListNode head) {
        if(head == null || head.next == null) return head;
        ListNode header = new ListNode(Integer.MIN_VALUE);
        header.next = head;
        ListNode cur = head.next;
        head.next = null;
        while(cur != null){
            ListNode tmp = header;
            while(tmp.next != null && tmp.next.val <= cur.val){
                tmp = tmp.next;
            }
            ListNode next = cur.next;
            cur.next = tmp.next;
            tmp.next = cur;
            cur = next;
        }
        return header.next;
    }
}

② 在discuss中看到的,类似于归并排序,将链表分为前后两个链表,然后通过比较依次插入到链表中。

class Solution {
    public ListNode insertionSortList(ListNode head) {
        if(head == null || head.next == null) return head;
        //链表对折
        ListNode slow = head;//用于表示后半部分
        ListNode fast = head;//用于记数
        ListNode tmp = null;//tmp临时存储(类似pre)
        while(fast != null && fast.next != null){
            tmp = slow;
            slow = slow.next;
            fast = fast.next.next;
        }
        ListNode l2 = insertionSortList(slow);//递归使后半部分有序
        tmp.next = null;//断开链表,将其分为两部分
        ListNode l1 = insertionSortList(head);//递归使前半部分有序
        ListNode header = new ListNode(-1);
        ListNode cur = header;
        //将前半部分和后半部分比较
        while(l1 != null && l2 != null){
            if (l1.val < l2.val) {
                cur.next = l1;
                l1 = l1.next;
            }else{
                cur.next = l2;
                l2 = l2.next;
            }
            cur = cur.next;
        }
        if(l1 != null) cur.next = l1;
        if(l2 != null) cur.next = l2;
        return header.next;
    }
}

转载于:https://my.oschina.net/liyurong/blog/1563431

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是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、付费专栏及课程。

余额充值