排序链表- python-非进阶做法

题目:

思路:

借助list

  1. 新建list()

  2. 遍历链表,把数字加到list中

  3. 调用list的排序函数进行排序

  4. 把排序后的元素放到链表中

    Definition for singly-linked list.

    class ListNode:

    def init(self, val=0, next=None):

    self.val = val

    self.next = next

    class Solution:
    def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
    new_list = list()
    curr = head
    while curr:
    new_list.append(curr.val)
    curr = curr.next
    new_list.sort()
    dump = ListNode(-1,head)
    temp = dump
    for num in new_list:
    new_node = ListNode(num)
    temp.next = new_node
    temp = temp.next
    return dump.next

相关推荐
YuTaoShao4 小时前
【LeetCode 每日一题】3010. 将数组分成最小总代价的子数组 I——(解法二)排序
算法·leetcode·排序算法
吴维炜5 小时前
「Python算法」计费引擎系统SKILL.md
python·算法·agent·skill.md·vb coding
Σίσυφος19006 小时前
PCL Point-to-Point ICP详解
人工智能·算法
you-_ling6 小时前
数据结构:4.二叉树
数据结构
玄〤7 小时前
Java 大数据量输入输出优化方案详解:从 Scanner 到手写快读(含漫画解析)
java·开发语言·笔记·算法
weixin_395448917 小时前
main.c_cursor_0202
前端·网络·算法
senijusene7 小时前
数据结构与算法:队列与树形结构详细总结
开发语言·数据结构·算法
青桔柠薯片7 小时前
数据结构:队列,二叉树
数据结构
杜家老五7 小时前
综合实力与专业服务深度解析 2026北京网站制作公司六大优选
数据结构·算法·线性回归·启发式算法·模拟退火算法
寄存器漫游者7 小时前
数据结构:带头节点单链表
c语言·数据结构