BM1 反转链表
2025-3-2
| 2025-3-2
0  |  阅读时长 0 分钟
type
status
date
slug
summary
tags
category
icon
password

描述

给定一个单链表的头结点pHead(该头节点是有值的,比如在下图,它的val是1),长度为n,反转该链表后,返回新链表的表头。
数据范围: 0≤n≤10000≤n≤1000
要求:空间复杂度 O(1),时间复杂度 O(n) 。
如当输入链表{1,2,3}时,
经反转后,原链表变为{3,2,1},所以对应的输出为{3,2,1}。
以上转换过程如下图所示:
notion image

题解

  1. 如果是空
    1. 返回
  1. 如果不是空
    1. 初始化pre为空
    2. while loop
      1. tmp = next
      2. cur.next = pre
      3. pre = cur
      4. cur = tmp
    3. return pre
    4.  
  • linux
  • 开发
  • BM2 链表内指定区间反转certbot获取证书
    Loading...
    目录