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

力扣刷题-热题100题-第23题(c++、python)

206. 反转链表 - 力扣(LeetCode)https://leetcode.cn/problems/reverse-linked-list/solutions/551596/fan-zhuan-lian-biao-by-leetcode-solution-d1k2/?envType=study-plan-v2&envId=top-100-liked

常规法

记录前一个指针,当前指针,后一个指针,遍历链表,改变指针的指向。

//c++
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) 
    {
        ListNode *pre=nullptr;
        while(head)
        {
            ListNode *nex=head->next;
            head->next=pre;
            pre=head;
            head=nex;
        }
        return pre;
    }
};

#python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        pre=None
        while head:
            nex=head.next
            head.next=pre
            pre=head
            head=nex
        return pre  

递归法 

对于每一个元素的下一个的下一个要指向自己,自己再指向空,递归进去得到头指针。

//c++
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* reverseList(ListNode* head) 
    {
        if(!head||!head->next)  return head;
        ListNode * ne=reverseList(head->next);
        head->next->next=head;
        head->next=nullptr;
        return ne;
    }
};

#python
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
        if not head or not head.next:
            return head
        new=self.reverseList(head.next)
        head.next.next=head;
        head.next=None;
        return new  

相关文章:

  • 股指期权最后交易日是哪一天?
  • tortoiseSVN、source insignt、J-flash使用
  • 算法 | 蜣螂优化算法原理,引言,公式,算法改进综述,应用场景及matlab完整代码
  • C语言笔记(鹏哥)上课板书+课件汇总(动态内存管理)--数据结构常用
  • 在fedora41中使用最新版本firefox和腾讯翻译插件让英文网页显示中文翻译
  • package-lock.json能否直接删除?
  • Java 集合 List、Set、Map 区别与应用
  • vue 一个组件的开发,引出组件开发流程和知识点
  • 职坐标:互联网行业职业发展路径解析
  • CSS 相对复杂但实用的margin
  • 手动创建Electron+React项目框架(建议直接看最后)
  • vue3里面使用Socketjs之后打包完访问的时候报socketStore-BmspPEpN.js:1 WebSocket connection to
  • HarmonyOS Next应用架构设计与模块化开发详解
  • 数据:$UPC 上涨突破 5.8 USDT,近7日总涨幅达 73.13%
  • 常见中间件漏洞攻略-Tomcat篇
  • Spring Boot定时任务设置与实现
  • 5.3 位运算专题:LeetCode 371. 两整数之和
  • 区块链驱动金融第十章——走进另类币与加密货币生态系统:比特币之外的广阔天地
  • 知识库外挂 vs 大脑全开:RAG与纯生成式模型(如GPT)的终极Battle
  • vue判断组件是否有传入的slot,有就渲染slot,没有就渲染内部节点默认内容
  • 女冰队长于柏巍,拒绝被年龄定义
  • 屠呦呦当选美国科学院外籍院士
  • 国泰海通合并后首份业绩报告出炉:一季度净利润增逾391%
  • 邮储银行一季度净赚超252亿降逾2%,营收微降
  • 卡尼领导的加拿大自由党在联邦众议院选举中获胜
  • 河北:开展领导干部任性用权等形式主义官僚主义问题专项整治