首页 > 其他分享 >147. 对链表进行插入排序(中)

147. 对链表进行插入排序(中)

时间:2024-03-03 11:13:05浏览次数:24  
标签:pre 147 cur dummy 插入排序 next 链表 节点

目录

题目

  • 给定单个链表的头 head ,使用 插入排序 对链表进行排序,并返回 排序后链表的头 。

    插入排序 算法的步骤:

  • 插入排序是迭代的,每次只移动一个元素,直到所有元素可以形成一个有序的输出列表。

  • 每次迭代中,插入排序只从输入数据中移除一个待排序的元素,找到它在序列中适当的位置,并将其插入。

  • 重复直到所有输入数据插入完为止。

题解

  • 创建一个新链表用于存储排序好的结果,对题目告知的原链表一个一个取节点,进行判断,放到新链表
class Solution:
    def insertionSortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        dummy = ListNode(0)  # 创建哑节点作为新链表的头部
        i = head  # 从原链表的头部开始遍历
        j = dummy  # 新链表的指针
        while i is not None:
            next_node = i.next  # 记录下一个节点,以便继续遍历新链表
            # 在新链表中找到适当的位置插入节点i
            while j.next is not None and j.next.val < i.val:
                j = j.next
            # 将节点i插入到新链表中
            i.next = j.next
            j.next = i
            i = next_node  # 更新i为下一个节点,继续遍历链表1
            j = dummy  # 将j重置为新链表的头部
        return dummy.next  # 返回排序好的新链表

优化

class Solution:
    def insertionSortList(self, head: ListNode) -> ListNode:
        # 找个排头
        dummy = ListNode(float("-inf"))
        pre = dummy
        tail = dummy
        # 依次拿head节点
        cur = head
        while cur:
            #因为测试案例很多是有序的,尾指针可以避免从头开始,所以会快很多,这里就是优化所在
            if tail.val < cur.val:#当原链表的值大于新链表尾节点值时,直接插在新链表尾部
                tail.next = cur
                tail = cur
                cur = cur.next
            else:#当原链表的值小于新链表尾节点,需要从新链表头开始找到它插入的位置
                tmp = cur.next # 把下一次节点保持下来,方便更新
                # 找到插入的位置
                while pre.next and pre.next.val < cur.val:
                    pre = pre.next
                # 进行插入操作
                cur.next = pre.next
                pre.next = cur
                pre= dummy#pre重置为新链表的头部
                cur = tmp#更新cur
        tail.next=None#最后记得把尾指针指向空
        return dummy.next

标签:pre,147,cur,dummy,插入排序,next,链表,节点
From: https://www.cnblogs.com/lushuang55/p/18049708

相关文章

  • leedcode 相交链表
    会超出时间限制:classSolution:defgetIntersectionNode(self,headA:ListNode,headB:ListNode)->Optional[ListNode]:cur_b=headBcur_a=headAwhilecur_b!=None:#两个相等ifcur_b==cur_a:r......
  • 【LeetCode】876_链表的中间结点_C
    题目描述给你单链表的头结点head,请你找出并返回链表的中间结点。如果有两个中间结点,则返回第二个中间结点。https://leetcode.cn/problems/middle-of-the-linked-list/description/示例提示:链表的结点数范围是[1,100]1<=Node.val<=100解题思路思路一遍历链......
  • Leetcode刷题第十五天-链表
    203:移除链表元素链接:203.移除链表元素-力扣(LeetCode)#Definitionforsingly-linkedlist.#classListNode:#def__init__(self,val=0,next=None):#self.val=val#self.next=nextclassSolution:defremoveElements(self,head:Op......
  • 【C++】相对于数组,在链表中添加和删除元素更容易,但排序速度更慢。这就引出了一种可能
    相对于数组,在链表中添加和删除元素更容易,但排序速度更慢。这就引出了一种可能性:相对于使用链表算法进行排序,将链表复制到数组中,对数组进行排序,再将排序后的结果复制到链表中的速度可能更快;但这也可能占用更多的内存。请使用如下方法检验上述假设。a.创建大型vector<int>对象vi0,并......
  • 142. 环形链表 II C
    /***Definitionforsingly-linkedlist.*structListNode{*intval;*structListNode*next;*};*/structListNode*detectCycle(structListNode*head){if(!head||!head->next)returnNULL;structListNode*slow=head;s......
  • 面试题 02.07. 链表相交C
    利用链表的特性,如果相交的话,后面就不可能岔开!你可以想象把他们有同一个尾巴,然后从尾巴往前看。所以只要知道两个链表的长度,就可以在同一起跑线上一起比较了。/***Definitionforsingly-linkedlist.*structListNode{*intval;*structListNode*next;......
  • 19. 删除链表的倒数第 N 个结点C
    /***Definitionforsingly-linkedlist.*structListNode{*intval;*structListNode*next;*};*/structListNode*removeNthFromEnd(structListNode*head,intn){if(!head)returnNULL;inti=0;structListNode*tem=head;......
  • 【力扣】奇偶链表
    题目描述思路我想起了一位故人。。前面那道分隔链表的题,只需要把<x的条件改为位置的奇偶即可完全照搬过来,出题人偷懒了属于是。试着不抄代码重新写一遍:简单写了一下发现这道题不太适合用递归算法求解,因为结点在整个链表中的位置不太好确认,试着用双指针法写一下:classSolut......
  • 203. 移除链表元素C
    写了个递归/***Definitionforsingly-linkedlist.*structListNode{*intval;*structListNode*next;*};*/structListNode*delect(structListNode*head,intx){if(!head)returnNULL;if(head->val==x){structListNode*......
  • leedcode 环形链表
    快慢指针:classSolution:defhasCycle(self,head:Optional[ListNode])->bool:#如果链表为空或者只有一个节点,肯定不存在环ifnotheadornothead.next:returnFalse#初始化慢指针和快指针slow=headf......