码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【数据结构】链表经典oj


    目录

    一、删除链表中等于val值的所有节点

     思路一

    思路二

    二、反转链表

    思路一

    思路二

    三、合并有序链表


     

    一、删除链表中等于val值的所有节点

     思路一

    就是将链表遍历一遍,对比,并将与val相同的链表删除。

    删除的具体步骤:1、将待删节点前一个指向待删节点后一个。2、然后释放val节点 。

     我们通过双指针的方式实现:

     

    1. struct ListNode* removeElements(struct ListNode* head, int val){
    2. struct ListNode *cur=head,*prev=NULL;
    3. while(cur)
    4. {
    5. if(cur->val==val)
    6. {
    7. //当删除头节点时
    8. if(cur==head)
    9. {
    10. head=head->next;
    11. free(cur);
    12. cur=head;
    13. }
    14. else
    15. {
    16. prev->next=cur->next;
    17. free(cur);
    18. cur=prev->next;
    19. }
    20. }
    21. //保持两个指针一前一后
    22. else
    23. {
    24. prev=cur;
    25. cur=cur->next;
    26. }
    27. }
    28. return head;
    29. }

    思路二

     

     创建一个新的头结点,然后通过一个cur指针扫描链表,将不等于val的节点尾插到phead后面。如下图

     

     

     

    1. struct ListNode* removeElements(struct ListNode* head, int val){
    2. struct ListNode *newhead=(struct ListNode *)malloc(sizeof(struct ListNode));
    3. newhead->next=NULL;
    4. struct ListNode *cur=head,*del=NULL,*tail=newhead;
    5. while(cur)
    6. {
    7. if(cur->val!=val)
    8. {
    9. tail->next=cur;
    10. cur=cur->next;
    11. tail=tail->next;
    12. }
    13. else
    14. {
    15. del=cur;
    16. cur=cur->next;
    17. free(del);
    18. }
    19. }
    20. if(tail)//防止传入空链表导致
    21. 越界
    22. tail->next=NULL;
    23. return newhead->next;
    24. }

    二、反转链表

    思路一

     简简单单的对原链表进行头插

     将原链表一个个头插在newhead节点后:

    c++字典列表
    rar 0星 超过10%的资源 5KB
    下载

     

     以此类推

    1. struct ListNode* reverseList(struct ListNode* head){
    2. struct ListNode *cur=head,*next=NULL,* newhead=(struct ListNode *)malloc(sizeof(struct ListNode));
    3. newhead->next=NULL;
    4. while(cur)
    5. {
    6. newhead->next=cur;
    7. cur=cur->next;
    8. newhead->next->next=next;
    9. next=newhead->next;
    10. }
    11. return newhead->next;
    12. }

    思路二

    将原链表逆序。

     我们需要三个指针,少一个都不行,第一步:

     第二步:

     以此类推,知道cur=NULL

    1. struct ListNode* reverseList(struct ListNode* head){
    2. struct ListNode * prev,*cur,*next=NULL;
    3. prev=NULL;
    4. cur=head;
    5. next=NULL;
    6. while(cur)
    7. {
    8. next=cur->next;
    9. cur->next=prev;
    10. //迭代
    11. prev=cur;
    12. cur=next;
    13. }
    14. return prev;
    15. }

    三、合并有序链表

     思路:

    将两个链表取小值尾插入下面的头节点。

    我的经典表结构
    application/msword 0星 超过10%的资源 48KB
    下载

     

    1. struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
    2. struct ListNode *head=(struct ListNide *)malloc(sizeof(struct ListNode));
    3. struct ListNode *tail=head;
    4. while(list1&&list2)
    5. {
    6. //判断大小+尾接
    7. if(list1->val<=list2->val)
    8. {
    9. tail->next=list1;
    10. list1=list1->next;
    11. }
    12. else
    13. {
    14. tail->next=list2;
    15. list2=list2->next;
    16. }
    17. tail=tail->next;
    18. }
    19. //将剩下的链表一起接上
    20. if(list1==NULL)
    21. {
    22. tail->next=list2;
    23. }
    24. if(list2==NULL)
    25. {
    26. tail->next=list1;
    27. }
    28. return head->next;
    29. }

    下一期还会有其他几道金典链表oj

  • 相关阅读:
    这可能是最全的SpringBoot3新版本变化了!
    gRPC调试, 用 Apipost
    Java练习题 二
    云原生技术-微服务SpringCloud(1)
    数据库概述01
    多线程常识相关
    Github 摸鱼神器面世!
    最新版本 Stable Diffusion 开源 AI 绘画工具之汉化篇
    BAT030:按列表名单将路径a下的文件夹批量复制到路径b
    分布式系统的发展历程
  • 原文地址:https://blog.csdn.net/qq_64484137/article/details/126308166
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号