当前位置: 首页 > news >正文

Python | Leetcode Python题解之第430题扁平化多级双向链表

题目:

题解:

class Solution:def flatten(self, head: "Node") -> "Node":def dfs(node: "Node") -> "Node":cur = node# 记录链表的最后一个节点last = Nonewhile cur:nxt = cur.next# 如果有子节点,那么首先处理子节点if cur.child:child_last = dfs(cur.child)nxt = cur.next# 将 node 与 child 相连cur.next = cur.childcur.child.prev = cur# 如果 nxt 不为空,就将 last 与 nxt 相连if nxt:child_last.next = nxtnxt.prev = child_last# 将 child 置为空cur.child = Nonelast = child_lastelse:last = curcur = nxtreturn lastdfs(head)return head

http://www.mrgr.cn/news/33902.html

相关文章:

  • YOLO航拍车辆和行人识别
  • 实战篇 | WSL迁移Linux系统到非系统盘(完整实操版)
  • 旋转机械故障数据集 全网首发
  • 自然语言处理的算法:从SVM到Attention
  • UIKit-Camera
  • 滚动轴承故障诊断、预测与分类综合数据集
  • C语言 | Leetcode C语言题解之第430题扁平化多级双向链表
  • 全网最适合入门的面向对象编程教程:51 Python函数方法与接口-使用Zope实现接口
  • C++ | Leetcode C++题解之第429题N叉树的层序遍历
  • 6.7泊松噪声
  • 安装 Anaconda
  • Renesas R7FA8D1BH (Cortex®-M85)的 General PWM的应用实践
  • OSError: Missing dependencies for SOCKS support
  • Java数据库连接——JDBC
  • 智能农业系统——土壤养分运移转化
  • 一些迷你型信息系统 - 2
  • 如何在 MySQL Workbench 中修改表数据并保存??
  • 华为杯”第十二届中国研究生数学建模竞赛-B题: 数据的多流形结构分析
  • Hive之任务优化
  • 【Android】 IconFont的使用