147. 对链表进行插入排序

代码:

# Definition for singly-linked list.
class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None

class Solution:
    def insertionSortList(self, head: ListNode) -> ListNode:
        if not head:
            return head

        cur, nxt = head, head.next
        dummy = ListNode('-inf')
        dummy.next = head
        while nxt:#这里是nxt
            if nxt.val > cur.val:#这里用if
                cur, nxt = nxt, nxt.next
            else:
                pre, pre2 = dummy, dummy.next
                cur.next = nxt.next#nxt的值小于cur的值。断开cur与nxt,cur指向nxt的下一个
                while nxt.val > pre2.val:
                    pre, pre2 = pre2, pre2.next
                pre.next = nxt
                nxt.next = pre2
                nxt = cur.next#这里是cur.next
        return dummy.next

head = ListNode(4)
a = ListNode(3)
b = ListNode(2)
c = ListNode(1)
head.next = a
a.next = b
b.next = c
new_head = Solution().insertionSortList(head)
while new_head:
    print(new_head.val)
    new_head = new_head.next

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
插入排序也是一种简单有效的排序算法,对于链表的排序也可以采用插入排序算法。 C 语言实现代码如下: ```c #include <stdio.h> #include <stdlib.h> // 链表节点结构体 struct ListNode { int val; struct ListNode *next; }; // 插入排序函数 struct ListNode* insertionSortList(struct ListNode* head) { if (head == NULL || head->next == NULL) { return head; } struct ListNode *newHead = (struct ListNode *)malloc(sizeof(struct ListNode)); newHead->val = -1; newHead->next = head; struct ListNode *cur = head->next, *pre = head; while (cur != NULL) { if (cur->val >= pre->val) { pre = cur; cur = cur->next; } else { struct ListNode *p = newHead; while (p->next->val < cur->val) { p = p->next; } pre->next = cur->next; cur->next = p->next; p->next = cur; cur = pre->next; } } return newHead->next; } // 创建链表 struct ListNode* createList(int arr[], int n) { struct ListNode *head = NULL, *cur = NULL; for (int i = 0; i < n; i++) { struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode)); node->val = arr[i]; node->next = NULL; if (head == NULL) { head = node; } else { cur->next = node; } cur = node; } return head; } // 打印链表 void printList(struct ListNode* head) { while (head != NULL) { printf("%d ", head->val); head = head->next; } printf("\n"); } int main() { int arr[] = {4, 2, 1, 3}; int n = sizeof(arr) / sizeof(int); struct ListNode *head = createList(arr, n); printf("Original list: "); printList(head); head = insertionSortList(head); printf("Sorted list: "); printList(head); return 0; } ``` 在这个代码中,我们定义了一个 `ListNode` 结构体表示链表节点,其中包含一个 `val` 字段和一个 `next` 指针指向下一个节点。 我们使用 `insertionSortList` 函数对链表进行插入排序。在函数中,我们首先创建一个新的头节点 `newHead`,将其值设为 `-1`,将其 `next` 指向原链表的头节点 `head`。 接下来,我们使用两个指针 `pre` 和 `cur` 分别指向已排序部分的末尾节点和未排序部分的头节点。如果 `cur` 的值大于等于 `pre` 的值,则将 `pre` 和 `cur` 向后移动一个节点;否则,我们需要将 `cur` 插入到已排序部分中。 为了找到 `cur` 应该插入的位置,我们使用一个指针 `p` 遍历已排序部分。如果 `p->next->val` 小于 `cur->val`,则将 `p` 向后移动一个节点;否则,我们将 `cur` 插入到 `p` 后面。 最后,我们返回 `newHead->next`,即为排序后的链表。 通过 `createList` 函数创建链表,并通过 `printList` 函数打印链表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值