leetcode: Reverse Linked List

飞书日历同步Outlook翻车?我踩过的坑和最终用Google Calendar成功的完整配置记录 本文详细记录了从Outlook日历同步到飞书时遇到的常见问题及解决方案,特别是通过Google Calendar实现稳定同步的完整配置流程。文章揭示了Outlook同步方案易翻车的原因,并提供了Google Calendar的避坑指南、高级技巧和企业级部署建议,帮助用户高效实现日历同步。 阅读详情

我去,单链表的逆转都没搞定,,,醉了

 

写的啥破玩意儿,,, 还是写不出来。

能出结果,但不知道哪里错了,没被ac, 太low了。

low写法:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */

/*
思路:
1.
*/
// class Solution {
// public:
//     ListNode* reverseList(ListNode* head) {
     
//         ListNode* Ppre=head;
//          ListNode* Pcur=head->next;
//         //ListNode* Pnext=Pcur->next;
//          head->next=NULL;
            
//         if(NULL == Pcur->next){
//             Pcur->next=Ppre;
//             return Pcur;
//         }
//         else{
//             head=reverseList(Pcur);
//             // head->next=NULL;
//         return head;
//         }
       
//     }
// };

class Solution {
public:
    int length(ListNode* head) {
     
        int len=1;
        while(NULL!=head->next)
        {
            len++;
            head=head->next;
            //printf("%d  ",len);
        }
        return len;
        
    }
    
    
    ListNode* reverseList(ListNode* head) {
     ListNode* head_orig=head;
        int len=length(head);
        int arry[len];
       
        // vector<int>& nums;

        for(int i=0;i<len;i++)
        // while(NULL!=head)
        {
            arry[i]=head->val;
            head=head->next;
            printf("%d  ",arry[i]);
        }
        
        head=head_orig;
        len--;
        //for(int j=0;j<len+1;j++)
        while(NULL!=head)
        {
            printf("==%d  \n",arry[len]);
           head->val=arry[len--];
            head=head->next;
        }
        
        return head_orig;
        
    }
    
    
};

 

 

等下用正规的方法吧,,,

 

 

 

看了别人之后的写法;

/*
迭代思路:
1.空链表,或者只有一个元素,直接返回head
2.其他:
新定义几个中间变量,用头插法,
cur_head, head_next,head
*/


class Solution {
public:
    ListNode* reverseList(ListNode* head) {
     if(NULL==head || NULL==head->next) return head;
        
        ListNode *cur_head=NULL,*head_next=NULL;
        while(head)
        {
            head_next = head->next;//save next
            
            //头插,插在cur_head的前面
            head->next= cur_head;//point to current head, insert in front of 
            cur_head=head;//modify the first element,cur_head 修改最前面的头的位置,以便下次头插指向
            
            head=head_next;//修改未遍历的头
            
        }
        return cur_head;//返回最前面的头
    }
};

 

总结:

1.名字命名的不直观会特别影响理解和思路,,,努力把名字命名得简易直接明了。

2.分类讨论,简单化问题。

3.分析能力也太差了吧,先理清具体思路,每一步,再动手写。

 

 

醉了,递归也不会写。

//make your thinking clearly before coding
/*
递归思路:
1.走到最后一个元素之后,翻转指向
2.意味着,前面的每一层都得保存着,保存的信息应该包括 ,前一个节点,
3.分三类思考:分别分析判断,
第1个节点,最后将next域指向NULL(if后)
最后一个节点,返回本节点,并将next域指向head_pre
中间的普通节点,
递去:保存pre_node,head=head->next,head_orig
归来:(条件,到最后一个元素了) cur_node=reverse(head->next),cur_node->next=pre_node

*/
class Solution {
public:
    ListNode* reverseList(ListNode* head) {
     
       ListNode *pre_save_node=NULL, *cur_node=NULL,*head_orig=NULL,*head_final=NULL;
         
        //if(NULL==head) return head;
        if( NULL==head->next)
        {//到最后了,归,只来一次
            head->next=pre_save_node;
            // head=pre_save_node;
            head_final=head;
            // cur_node=head->next;
             cur_node=pre_save_node;
            printf("comeback:%d\n",head->val);
            return cur_node;
        }
        
        pre_save_node=head;//存起来,后面作为下一个节点的next
        printf("comeback:%d\n",pre_save_node->val);
        cur_node=reverseList(head->next);
        printf("====\n");
        // printf("comeback:%d\n",cur_node->val);
        if(NULL!=head_orig) cur_node->next=head;
        // printf("====\n");
        return head_final;
       
    }
};

这么复杂还叫递归吗???

重新分析:

递归肯定是极度简洁的,

看别人家的:

//make your thinking clearly before coding
/*
递归思路:
1.走到最后一个元素之后,翻转指向
2.意味着,前面的每一层都得保存着,保存的信息应该包括 ,前一个节点,
3.分三类思考:分别分析判断,
第1个节点,最后将next域指向NULL(if后)
最后一个节点,返回本节点,并将next域指向head_pre
中间的普通节点,
递去:保存pre_node,head=head->next,head_orig
归来:(条件,到最后一个元素了) cur_node=reverse(head->next),cur_node->next=pre_node

*/
// class Solution {
// public:
//     ListNode* reverseList(ListNode* head) {
     
//        ListNode *pre_save_node=NULL, *cur_node=NULL,*head_orig=NULL,*head_final=NULL;
         
//         //if(NULL==head) return head;
//         if( NULL==head->next)
//         {//到最后了,归,只来一次
//             head->next=pre_save_node;
//             // head=pre_save_node;
//             head_final=head;
//             // cur_node=head->next;
//              cur_node=pre_save_node;
//             printf("comeback:%d\n",head->val);
//             return cur_node;
//         }
        
//         pre_save_node=head;//存起来,后面作为下一个节点的next
//         printf("comeback:%d\n",pre_save_node->val);
//         cur_node=reverseList(head->next);
//         printf("====\n");
//         // printf("comeback:%d\n",cur_node->val);
//         if(NULL!=head_orig) cur_node->next=head;
//         // printf("====\n");
//         return head_final;
       
//     }
// };



class Solution {
public:
    ListNode* reverseList(ListNode* head) {
      if(NULL==head || NULL==head->next) return head;
         ListNode *new_head=NULL,*head_next=NULL;
        head_next=head->next;
        head->next=NULL;//递归前切断
        new_head=reverseList(head_next);//递归
      head_next->next=head;//递归后反过来链接,改变指向
        return new_head;
    }
};


 

总结:肯定是过程没分析清,对递归根本不理解。

 

 

国科大学习资料--计算机视觉(胡占义、董秋雷)--2018-2019第二学期期末试卷 国科大学习资料–计算机视觉(胡占义、董秋雷)–2018-2019第二学期期末试卷 国科大学习资料–计算机视觉(胡占义、董秋雷)–2018-2019第二学期期末试卷 国科大学习资料–计算机视觉(胡占义、董秋雷)–2018-2019第二学期期末试卷 ... 阅读详情

相关推荐

Monocle2拟时轨迹基因模块的GO富集解析

本文详细解析了如何对Monocle2拟时分析中得到的基因表达模块进行GO富集分析。通过从热图对象中提取聚类模块,并利用clusterProfiler等工具进行功能注释,将抽象的基因表达时序模式转化为可解释的生物学功能主题,如免疫应答、细胞外基质组织等,从而深入理解细胞命运转变的驱动机制。

gpt4scribbler的博客 500

leetcode 206: Reverse Linked List

Reverse Linked List leetcode

西施豆腐渣 1万+

为什么你的Nginx配置SM2国密SSL证书失败?这些坑我都帮你踩过了

本文针对Nginx配置SM2国密SSL证书的常见失败原因,提供了详细的排查与解决方案。核心在于使用支持国密算法的Nginx版本(如集成Tongsuo库),正确处理从阿里云等平台获取的证书格式,并正确配置ssl_ciphers等关键参数。文章还解释了浏览器兼容性差异及双证书部署等进阶场景,帮助运维和开发人员高效完成国密合规改造。

weixin_29228203的博客 387

LeetCode206:Reverse Linked List

Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 使用迭代和递归实现单链表的反转。 迭代的方法之间在剑指offer上面

2248

【Gin框架入门到精通系列01】Gin框架介绍与环境搭建

本文是Gin框架入门到精通系列的第一篇,系统介绍Gin框架的基本概念、核心特性以及环境搭建。无论你是Go语言新手还是有经验的开发者,都能通过本文快速掌握Gin框架的基础知识,创建并运行你的第一个Gin应用。

GopherTribe的博客 1560

AIDD-人工智能药物设计-构建基于机器学习方法的在线预测平台高准确预测药物诱导骨毒性

在此基础上,提取多种药物分子特征,包括分子指纹(如MACCS、Morgan、RDKit、TopologicalTorsion、AtomPairs与Descriptors)及分子图谱,通过Transformer编码器与神经网络进行机器学习模型训练。最后,将训练好的模型部署为在线Web服务器(BoneToxPD),供用户快速预测药物的骨毒性。本研究不仅在骨毒性预测的准确性方面取得了突破,还在模型解释性方面提供了新的视角,通过注意力机制精准识别出影响骨毒性的关键化学基团,推动药物研发领域更安全、更高效的发展。

itwangyang520的博客 1081

Leetcode: Reverse Linked List

QuestionReverse a singly linked list.click to show more hints.Show Tags Show Similar ProblemsSolution 1Analysisrecursive methodCode# Definition for singly-linked list. # class ListNode(object): #

ayst123 的专栏 348

OpenClaw与大语言模型(Claude/GPT/MiniMax)的集成架构与最优配置策略

摘要 本文探讨了OpenClaw框架与大语言模型(Claude/GPT/MiniMax)的集成架构与优化策略。大语言模型具有强大的自然语言处理能力,但集成面临API成本、响应延迟、上下文管理等挑战。OpenClaw采用分层架构设计,包括用户界面层、对话管理层、提示工程层、模型适配层和API调用层,支持多模型切换、智能对话管理和成本控制。系统通过模型适配层实现不同API的统一调用,并提供缓存、监控等优化机制。该集成方案能够充分发挥大语言模型的优势,为本地AI助理提供更智能的服务。

一键难忘的博客 1693

LeetCode:Reverse Linked List Java实现

LeetCode题目:reverse linked list 题目概述: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL 解题方法 迭代: public ListNode reverseList(ListNode head) { if (head==nul...

yanhui的博客 216

Jetson Nano之CSI摄像头操作方法

jetson端csi摄像头的应用方法

ZONGXP的博客 1万+

SAP 系统间 SSO 的真正价值,从 SM59 到可信 RFC 的安全设计

SAP S/4HANA系统间Single Sign-On(SSO)的核心价值在于集中管理通信身份与信任关系,而非仅减少密码输入。传统固定账号密码方式在系统规模扩大后会面临密码管理、审计追踪等挑战。通过SM59配置可信RFC目标系统,结合SSL证书或SAP Trusted System等强认证机制,能有效降低安全凭据暴露风险。SSO配置需配合授权对象、角色管理等建立清晰的信任边界,实现系统间安全通信的同时保持可审计性。这种方案特别适用于Fiori前端与S/4HANA后端等场景,通过可信RFC传递用户身份,避免

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1063

LeetCode:Reverse Linked List

问题描述:Reverse a singly linked list.顾名思义,就是将单链表反转,这其实一道很简单的算法,我在前面的博客也写过解法,以前是刚看过《剑指offer》,所以对那上面的解法印象较深。 解法一: 参考代码如下: 结构体定义: struct NodeList {     int value;     struct NodeList  next;

前端产品工程师 345

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Leetcode: Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy t

ZkvIA的博客 2044

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

LeetCode之“链表”:Reverse Linked List && Reverse Linked List II

  1.Reverse Linked List   题目链接   题目要求:   Reverse a singly linked list.   Hint:   A linked list can be reversed either iteratively or recursively. Could you implement both?   初想用递归来实现应该会挺好的...

98

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

LeetCode题解: Reverse Linked List II

Reverse Linked List II Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note:

Magi Su的技术回收站 3841

Leetcode 206: Reverse Linked List

问题描述 Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL Follow up: A linked list can be reversed either iteratively or recursiv...

zero 227

Leetcode: Reverse Linked List

linkedlist,iterative seems much easier than recursive Reverse Linked List https://leetcode.com/problems/reverse-linked-list/description/ /** * Definition for singly-linked list. * struct ListNode ...

weixin_43476349的博客 173

leetcode: 206. Reverse Linked List

leetcode: 206. Reverse Linked List 方法一:双指针法,不创建新的链! /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} *

LSK的博客 606

LeetCode: Reverse Linked List

Reverse a singly linked list. 源代码1: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ c

Lost Monkey 245

LeetCode:206. Reverse Linked List

LeetCode:206. Reverse Linked List Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL 翻转链表。 思路一:头插法 遍历链表,直接将元素插入到头部前面,最后链表就翻转...

Wisimer 239

[LeetCode] 088: Reverse Linked List II

[Problem] Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satis

1.02^365的成长裂变 3615

LeetCode 206:Reverse Linked List

Reverse a singly linked list. 单向链表的翻转,代码如下: ListNode* reverseList(ListNode* head) { if(head == NULL || head->next == NULL) return head; ListNode Result(0);

哈哈的个人专栏 2552

leetcode:Reverse Linked List II

Reverse Linked List II Total Accepted: 20087 Total Submissions: 77182 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NU

463

LeetCode:Reverse Linked List

Reverse a singly linked list. 思路: 注意几个地方: 变换前:   h  ---->    i   ---->    j 变换后:   h   1 以i节点为当前节点。假设i节点前面的节点都实现了逆转。现在已i节点为参考对象。注意需要用一个新定义的ListNode去记录下i的下一个节点j。因为如果单纯的将j的下一节点指向h,会导致i , j 之间断裂

u011974997的博客 202

《leetCode》:Reverse Linked List

题目Reverse a singly linked list.思路这个题也相当简单。借用接个指针来完成即可。/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* reverseList

wojiushimogui的博客 433

Leetcode: Reverse Linked List I

描述: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->

spirit_young的博客 369
上一篇: leetcode:283. Move Zeroes
下一篇: STL:C++ STL 几个容器的底层实现 收藏一下
nedwons
博客等级 码龄9年 311粉丝 572原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值